mirror of
https://github.com/ondra-novak/gates_of_skeldal.git
synced 2025-07-08 15:40:28 -04:00
trying to debug, additional rewrites
This commit is contained in:
parent
378b5586ab
commit
42f780a729
87 changed files with 1771 additions and 529 deletions
112
platform/sdl/BGraph2.cpp
Normal file
112
platform/sdl/BGraph2.cpp
Normal file
|
@ -0,0 +1,112 @@
|
|||
#include "BGraph2.h"
|
||||
|
||||
#include "sdl_context.h"
|
||||
#include "global_context.h"
|
||||
|
||||
|
||||
static std::unique_ptr<uint16_t[]> screen_buffer;
|
||||
static std::unique_ptr<uint16_t[]> buffer2nd;
|
||||
static uint16_t *render_target;
|
||||
static uint16_t screen_pitch = 640;
|
||||
|
||||
char DXInit64(char inwindow,int zoom,int monitor, int refresh) {
|
||||
|
||||
SDLContext::DisplayMode mode;
|
||||
if (inwindow) {
|
||||
if (zoom) {
|
||||
mode = SDLContext::double_window;
|
||||
} else {
|
||||
mode = SDLContext::native_window;
|
||||
}
|
||||
} else {
|
||||
mode = SDLContext::fullscreen;
|
||||
}
|
||||
|
||||
screen_pitch = 640;
|
||||
get_sdl_global_context().init_screen(mode, "Skeldal"); //todo allow change
|
||||
screen_buffer = std::make_unique<uint16_t[]>(screen_pitch*480);
|
||||
buffer2nd = std::make_unique<uint16_t[]>(screen_pitch*480);
|
||||
render_target = screen_buffer.get();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void DXCloseMode() {
|
||||
get_sdl_global_context().close_screen();
|
||||
}
|
||||
|
||||
|
||||
uint16_t *GetScreenAdr() {
|
||||
return render_target;
|
||||
}
|
||||
uint16_t *GetBuffer2nd() {
|
||||
return buffer2nd.get();
|
||||
|
||||
}
|
||||
int32_t GetScreenPitch() {
|
||||
return screen_pitch;
|
||||
}
|
||||
int32_t GetBuffer2ndPitch() {
|
||||
return screen_pitch;
|
||||
}
|
||||
int32_t GetScreenSizeBytes() {
|
||||
return screen_pitch * 480 * 2;
|
||||
}
|
||||
|
||||
void RedirectScreen(uint16_t *newaddr) {
|
||||
render_target = newaddr;
|
||||
}
|
||||
void RestoreScreen() {
|
||||
render_target = screen_buffer.get();
|
||||
}
|
||||
void RedirectScreenBufferSecond() {
|
||||
render_target = buffer2nd.get();
|
||||
}
|
||||
int DxGetResX() {
|
||||
return 640;
|
||||
}
|
||||
int DxGetResY() {
|
||||
return 480;
|
||||
}
|
||||
void setvesa_displaystart(int x,int y){
|
||||
|
||||
}
|
||||
void StripBlt(void *data, unsigned int startline, uint32_t width) {
|
||||
|
||||
unsigned short *start=startline*GetScreenPitch()+GetScreenAdr();
|
||||
while (width--)
|
||||
{
|
||||
memcpy(start,data,640*2);
|
||||
data=(void *)(reinterpret_cast<char *>(data)+get_sdl_global_context().get_surface_pitch());
|
||||
start=start+GetScreenPitch();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void DXCopyRects64(unsigned short x,unsigned short y,unsigned short xs,unsigned short ys) {
|
||||
get_sdl_global_context().present_rect(screen_buffer.get(), screen_pitch, x,y,xs,ys);
|
||||
|
||||
}
|
||||
|
||||
void *DxPrepareWalk(int ypos) {
|
||||
return 0;
|
||||
}
|
||||
void DxZoomWalk(void *handle, int ypos, int *points,float phase, void *lodka) {
|
||||
|
||||
}
|
||||
void DxDoneWalk(void *handle) {
|
||||
|
||||
}
|
||||
|
||||
void *DxPrepareTurn(int ypos) {
|
||||
return 0;
|
||||
}
|
||||
void DxTurn(void *handle, char right, int ypos,int border, float phase, void *lodka) {
|
||||
|
||||
}
|
||||
void DxDoneTurn(void *handle) {
|
||||
|
||||
}
|
||||
void DxTurnLeftRight(char right, float phase, int border, int ypos, int *last) {
|
||||
|
||||
}
|
63
platform/sdl/BGraph2.h
Normal file
63
platform/sdl/BGraph2.h
Normal file
|
@ -0,0 +1,63 @@
|
|||
#include <stdint.h>
|
||||
|
||||
#ifndef __BGRAPH_DX_WRAPPER_
|
||||
#define __BGRAPH_DX_WRAPPER_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
uint16_t *GetScreenAdr();
|
||||
uint16_t *GetBuffer2nd();
|
||||
int32_t GetScreenPitch();
|
||||
int32_t GetBuffer2ndPitch();
|
||||
int32_t GetScreenSizeBytes();
|
||||
|
||||
void RedirectScreen(uint16_t *newaddr);
|
||||
void RestoreScreen();
|
||||
void RedirectScreenBufferSecond();
|
||||
|
||||
|
||||
//inicializuje a otevira rezim 640x480x16b v DX - otevre okno, pripravi vse pro beh hry
|
||||
//Vraci 1 pri uspechu
|
||||
char DXInit64(char inwindow,int zoom,int monitor, int refresh);
|
||||
|
||||
//uzavre rezim grafiky
|
||||
void DXCloseMode();
|
||||
|
||||
//void DXCopyRects32(unsigned short x,unsigned short y,unsigned short xs,unsigned short ys);
|
||||
void DXCopyRects64(unsigned short x,unsigned short y,unsigned short xs,unsigned short ys);
|
||||
|
||||
void *DxPrepareWalk(int ypos);
|
||||
void DxZoomWalk(void *handle, int ypos, int *points,float phase, void *lodka);
|
||||
void DxDoneWalk(void *handle);
|
||||
|
||||
void *DxPrepareTurn(int ypos);
|
||||
void DxTurn(void *handle, char right, int ypos,int border, float phase, void *lodka);
|
||||
void DxDoneTurn(void *handle);
|
||||
void DxTurnLeftRight(char right, float phase, int border, int ypos, int *last);
|
||||
|
||||
|
||||
void DxDialogs(char enable);
|
||||
|
||||
void setvesa_displaystart(int x,int y);
|
||||
|
||||
|
||||
void DxSetInitResolution(int x, int y);
|
||||
int DxGetResX();
|
||||
int DxGetResY();
|
||||
|
||||
void DXMouseTransform(unsigned short *x, unsigned short *y);
|
||||
|
||||
//HWND GetGameWindow();
|
||||
//void DxLockBuffers(BOOL lock);
|
||||
|
||||
void StripBlt(void *data, unsigned int startline, uint32_t width);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
|
@ -1 +1,3 @@
|
|||
add_subdirectory(tests)
|
||||
|
||||
add_library(skeldal_sdl sdl_context.cpp BGraph2.cpp input.cpp sound.cpp)
|
11
platform/sdl/global_context.h
Normal file
11
platform/sdl/global_context.h
Normal file
|
@ -0,0 +1,11 @@
|
|||
#pragma once
|
||||
|
||||
#include "sdl_context.h"
|
||||
|
||||
inline SDLContext &get_sdl_global_context () {
|
||||
static SDLContext sdl_global_context;
|
||||
return sdl_global_context;
|
||||
}
|
||||
|
||||
|
||||
|
28
platform/sdl/input.cpp
Normal file
28
platform/sdl/input.cpp
Normal file
|
@ -0,0 +1,28 @@
|
|||
#include "global_context.h"
|
||||
#include "input.h"
|
||||
|
||||
char get_control_key_state() {
|
||||
return 0; //todo
|
||||
}
|
||||
char get_shift_key_state() {
|
||||
return 0; //todo
|
||||
}
|
||||
uint32_t _bios_keybrd(int mode) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void SetWheelMapping(char up, char down) { //todo
|
||||
|
||||
}
|
||||
|
||||
static MS_EVENT ms_event = {};
|
||||
|
||||
|
||||
|
||||
void get_ms_event(MS_EVENT *event) {
|
||||
*event = ms_event;
|
||||
}
|
||||
|
||||
void ShareCPU() {
|
||||
|
||||
}
|
31
platform/sdl/input.h
Normal file
31
platform/sdl/input.h
Normal file
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* input.h
|
||||
*
|
||||
* Created on: 26. 1. 2025
|
||||
* Author: ondra
|
||||
*/
|
||||
|
||||
#ifndef PLATFORM_SDL_INPUT_H_
|
||||
#define PLATFORM_SDL_INPUT_H_
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <devices.h>
|
||||
|
||||
|
||||
char get_control_key_state();
|
||||
char get_shift_key_state();
|
||||
uint32_t _bios_keybrd(int mode);
|
||||
void SetWheelMapping(char up, char down);
|
||||
void get_ms_event(MS_EVENT *event);
|
||||
void ShareCPU();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif /* PLATFORM_SDL_INPUT_H_ */
|
186
platform/sdl/sdl_context.cpp
Normal file
186
platform/sdl/sdl_context.cpp
Normal file
|
@ -0,0 +1,186 @@
|
|||
#include "sdl_context.h"
|
||||
#include "../platform.h"
|
||||
|
||||
#include <stdexcept>
|
||||
void SDLContext::SDL_Deleter::operator ()(SDL_Window* window) {
|
||||
SDL_DestroyWindow(window);
|
||||
}
|
||||
|
||||
void SDLContext::SDL_Deleter::operator ()(SDL_Renderer* renderer) {
|
||||
SDL_DestroyRenderer(renderer);
|
||||
}
|
||||
|
||||
void SDLContext::SDL_Deleter::operator ()(SDL_Surface* surface) {
|
||||
SDL_FreeSurface(surface);
|
||||
}
|
||||
|
||||
void SDLContext::SDL_Deleter::operator ()(SDL_Texture* texture) {
|
||||
SDL_DestroyTexture(texture);
|
||||
}
|
||||
|
||||
struct SDL_INIT_Context {
|
||||
|
||||
SDL_INIT_Context() {
|
||||
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO) < 0) {
|
||||
SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
|
||||
exit(1);
|
||||
}
|
||||
inited = true;
|
||||
}
|
||||
~SDL_INIT_Context() {
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
|
||||
bool inited = false;
|
||||
};
|
||||
|
||||
static SDL_INIT_Context init_context = {};
|
||||
|
||||
SDLContext::SDLContext() {
|
||||
if (!init_context.inited) throw std::runtime_error("SDL not inited");
|
||||
|
||||
}
|
||||
|
||||
void SDLContext::init_screen(DisplayMode mode, const char *title) {
|
||||
char buff[256];
|
||||
|
||||
int width = 640;
|
||||
int height = 480;
|
||||
if (mode == double_window) {
|
||||
width*=2;
|
||||
height*=2;
|
||||
}
|
||||
SDL_Window *window = SDL_CreateWindow(title,
|
||||
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
|
||||
width, height, SDL_WINDOW_RESIZABLE|(mode==fullscreen?SDL_WINDOW_FULLSCREEN_DESKTOP:0));
|
||||
|
||||
if (!window) {
|
||||
snprintf(buff, sizeof(buff), "SDL Error create window: %s\n", SDL_GetError());
|
||||
throw std::runtime_error(buff);
|
||||
}
|
||||
|
||||
_window.reset(window);
|
||||
SDL_Renderer *renderer = SDL_CreateRenderer(_window.get(), -1, 0);
|
||||
if (!renderer) {
|
||||
snprintf(buff,sizeof(buff), "Chyba při vytváření rendereru: %s\n", SDL_GetError());
|
||||
throw std::runtime_error(buff);
|
||||
}
|
||||
_renderer.reset(renderer);
|
||||
// Vytvoření softwarového backbufferu (surface)
|
||||
SDL_Surface *backbuffer = SDL_CreateRGBSurfaceWithFormat(0, 640, 480, 16, SDL_PIXELFORMAT_RGB565);
|
||||
if (!backbuffer) {
|
||||
snprintf(buff,sizeof(buff), "Chyba při vytváření surface: %s\n", SDL_GetError());
|
||||
throw std::runtime_error(buff);
|
||||
}
|
||||
_surface.reset(backbuffer);
|
||||
// Vytvoření textury pro zobrazení backbufferu
|
||||
SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB565, SDL_TEXTUREACCESS_STREAMING, 640, 480);
|
||||
if (!texture) {
|
||||
snprintf(buff, sizeof(buff), "Chyba při vytváření textury: %s\n", SDL_GetError());
|
||||
throw std::runtime_error(buff);
|
||||
}
|
||||
_texture.reset(texture);
|
||||
|
||||
SDL_LockSurface(_surface.get());
|
||||
|
||||
if (!_timer_event) _timer_event = SDL_RegisterEvents(1);
|
||||
_fullscreen_mode = mode == fullscreen;
|
||||
}
|
||||
|
||||
void SDLContext::close_screen() {
|
||||
SDL_UnlockSurface(_surface.get());
|
||||
_texture.reset();
|
||||
_surface.reset();
|
||||
_renderer.reset();
|
||||
_window.reset();
|
||||
|
||||
}
|
||||
|
||||
uint16_t* SDLContext::get_surface_addr() {
|
||||
return reinterpret_cast<uint16_t *>(_surface->pixels);
|
||||
}
|
||||
|
||||
|
||||
int32_t SDLContext::get_surface_pitch() {
|
||||
return _surface->pitch;
|
||||
}
|
||||
|
||||
|
||||
void SDLContext::charge_timer() {
|
||||
_active_timer = SDL_AddTimer(1000/TIMERSPEED, [](Uint32 interval, void *param) -> Uint32 {
|
||||
SDLContext *me = reinterpret_cast<SDLContext *>(param);
|
||||
SDL_Event* event = (SDL_Event*)param;
|
||||
event->type = me->_timer_event;
|
||||
SDL_PushEvent(event);
|
||||
return 0;
|
||||
}, this);
|
||||
|
||||
}
|
||||
void SDLContext::pool_events() {
|
||||
if (!_active_timer.has_value()) charge_timer();
|
||||
SDL_Event e;
|
||||
while (true) {
|
||||
if (SDL_WaitEvent(&e)) {
|
||||
if (e.type == SDL_QUIT) {
|
||||
_quit_requested = true;
|
||||
return;
|
||||
}
|
||||
if (e.type == _timer_event) {
|
||||
break;
|
||||
}
|
||||
if (e.type == SDL_KEYDOWN) {
|
||||
if (e.key.keysym.sym == SDLK_RETURN && (e.key.keysym.mod & KMOD_ALT)) {
|
||||
_fullscreen_mode = !_fullscreen_mode;
|
||||
SDL_SetWindowFullscreen(_window.get(), _fullscreen_mode ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
|
||||
}
|
||||
} else if (e.type == SDL_MOUSEMOTION) {
|
||||
int mouseX = e.motion.x;
|
||||
int mouseY = e.motion.y;
|
||||
int windowWidth;
|
||||
int windowHeight;
|
||||
SDL_GetWindowSize(_window.get(), &windowWidth, &windowHeight);
|
||||
float normalizedX = (float)mouseX / windowWidth;
|
||||
float normalizedY = (float)mouseY / windowHeight;
|
||||
ms_event.event = 1;
|
||||
ms_event.event_type = 1;
|
||||
ms_event.x = (int16_t)(640*normalizedX);
|
||||
ms_event.y = (int16_t)(480*normalizedY);
|
||||
} else if (e.type == SDL_MOUSEBUTTONDOWN || e.type == SDL_MOUSEBUTTONUP) {
|
||||
int mouseX = e.button.x;
|
||||
int mouseY = e.button.y;
|
||||
int button = e.button.button;
|
||||
int up = e.type == SDL_MOUSEBUTTONUP?1:0;
|
||||
ms_event.event = 1;
|
||||
ms_event.event_type = (1<<(2*button-1+up));
|
||||
switch (button) {
|
||||
default: break;
|
||||
case 1: ms_event.tl1 = !up; break;
|
||||
case 2: ms_event.tl2 = !up; break;
|
||||
case 3: ms_event.tl3 = !up; break;
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
throw std::runtime_error("SDL_WaitEvent error");
|
||||
}
|
||||
}
|
||||
charge_timer();
|
||||
|
||||
}
|
||||
|
||||
void SDLContext::present_rect(uint16_t *pixels, unsigned int pitch,
|
||||
unsigned int x, unsigned int y, unsigned int xs, unsigned ys) {
|
||||
|
||||
auto beg = pixels + y * pitch + y;
|
||||
SDL_Rect r = {static_cast<int>(x),
|
||||
static_cast<int>(y),
|
||||
static_cast<int>(xs),
|
||||
static_cast<int>(ys)};
|
||||
SDL_UpdateTexture(_texture.get(), &r, beg, pitch*2);
|
||||
SDL_RenderClear(_renderer.get());
|
||||
SDL_RenderCopy(_renderer.get(), _texture.get(), NULL, NULL);
|
||||
SDL_RenderPresent(_renderer.get());
|
||||
|
||||
|
||||
}
|
55
platform/sdl/sdl_context.h
Normal file
55
platform/sdl/sdl_context.h
Normal file
|
@ -0,0 +1,55 @@
|
|||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <SDL2/SDL.h>
|
||||
#include <mouse.h>
|
||||
|
||||
class SDLContext {
|
||||
public:
|
||||
|
||||
SDLContext();
|
||||
|
||||
enum DisplayMode {
|
||||
native_window,
|
||||
double_window,
|
||||
fullscreen
|
||||
};
|
||||
|
||||
|
||||
void init_screen(DisplayMode mode, const char *title);
|
||||
|
||||
void close_screen();
|
||||
|
||||
|
||||
uint16_t *get_surface_addr();
|
||||
int32_t get_surface_pitch();
|
||||
|
||||
|
||||
void pool_events();
|
||||
|
||||
void present_rect(uint16_t *pixels, unsigned int pitch, unsigned int x, unsigned int y, unsigned int xs,unsigned ys);
|
||||
|
||||
protected:
|
||||
|
||||
struct SDL_Deleter {
|
||||
void operator()(SDL_Window *);
|
||||
void operator()(SDL_Renderer *);
|
||||
void operator()(SDL_Surface *);
|
||||
void operator()(SDL_Texture *);
|
||||
};
|
||||
|
||||
MS_EVENT ms_event;
|
||||
|
||||
std::unique_ptr<SDL_Window, SDL_Deleter> _window;
|
||||
std::unique_ptr<SDL_Renderer, SDL_Deleter> _renderer;
|
||||
std::unique_ptr<SDL_Surface, SDL_Deleter> _surface;
|
||||
std::unique_ptr<SDL_Texture, SDL_Deleter> _texture;
|
||||
|
||||
std::optional<SDL_TimerID> _active_timer;
|
||||
Uint32 _timer_event = 0;
|
||||
|
||||
bool _quit_requested = false;
|
||||
bool _fullscreen_mode = false;
|
||||
void charge_timer();
|
||||
};
|
78
platform/sdl/sound.cpp
Normal file
78
platform/sdl/sound.cpp
Normal file
|
@ -0,0 +1,78 @@
|
|||
#include "../platform.h"
|
||||
#include <zvuk.h>
|
||||
|
||||
void set_mixing_device(int mix_dev,int mix_freq,...) {
|
||||
|
||||
}
|
||||
char start_mixing() {
|
||||
return 0;
|
||||
}
|
||||
void stop_mixing() {
|
||||
|
||||
}
|
||||
void play_sample(int channel,void *sample,int32_t size,int32_t lstart,int32_t sfreq,int type) {
|
||||
|
||||
}
|
||||
void set_channel_volume(int channel,int left,int right) {
|
||||
|
||||
}
|
||||
void set_end_of_song_callback(const char * (*cb)(void *), void *ctx) {
|
||||
|
||||
}
|
||||
|
||||
void fade_music() {
|
||||
|
||||
}
|
||||
int mix_back_sound(int synchro) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
//int open_backsound(char *filename);
|
||||
void change_music(const char *filename) {
|
||||
|
||||
}
|
||||
|
||||
//char *device_name(int device);
|
||||
//void force_music_volume(int volume);
|
||||
|
||||
//void set_backsnd_freq(int freq);
|
||||
|
||||
char get_channel_state(int channel) {
|
||||
return 0;
|
||||
}
|
||||
void get_channel_volume(int channel,int *left,int *right) {
|
||||
|
||||
}
|
||||
void mute_channel(int channel) {
|
||||
|
||||
}
|
||||
void chan_break_loop(int channel) {
|
||||
|
||||
}
|
||||
void chan_break_ext(int channel,void *org_sample,int32_t size_sample) {
|
||||
|
||||
}
|
||||
|
||||
char set_snd_effect(int funct,int data) {
|
||||
return 0;
|
||||
}
|
||||
char check_snd_effect(int funct) {
|
||||
return 0;
|
||||
}
|
||||
int get_snd_effect(int funct) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void *PrepareVideoSound(int mixfreq, int buffsize) {
|
||||
return 0;
|
||||
}
|
||||
char LoadNextVideoFrame(void *buffer, char *data, int size, short *xlat, short *accnums, int32_t *writepos) {
|
||||
return 0;
|
||||
|
||||
}
|
||||
void DoneVideoSound(void *buffer) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
15
platform/sdl/sound.h
Normal file
15
platform/sdl/sound.h
Normal file
|
@ -0,0 +1,15 @@
|
|||
/*
|
||||
* sound.h
|
||||
*
|
||||
* Created on: 26. 1. 2025
|
||||
* Author: ondra
|
||||
*/
|
||||
|
||||
#ifndef PLATFORM_SDL_SOUND_H_
|
||||
#define PLATFORM_SDL_SOUND_H_
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif /* PLATFORM_SDL_SOUND_H_ */
|
|
@ -1,6 +1,8 @@
|
|||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/tests/)
|
||||
|
||||
set(testFiles sdl_minimal_test.cpp sdl_surface_test.cpp
|
||||
set(testFiles sdl_minimal_test.cpp sdl_surface_test.cpp
|
||||
sdl_mouse.cpp
|
||||
sdl_update_texture.cpp
|
||||
)
|
||||
|
||||
foreach (testFile ${testFiles})
|
||||
|
|
79
platform/sdl/tests/sdl_mouse.cpp
Normal file
79
platform/sdl/tests/sdl_mouse.cpp
Normal file
|
@ -0,0 +1,79 @@
|
|||
#include <SDL2/SDL.h>
|
||||
#include <iostream>
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
|
||||
std::cerr << "SDL_Init Error: " << SDL_GetError() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
SDL_Window* window = SDL_CreateWindow("SDL Mouse Event Example", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_RESIZABLE);
|
||||
if (!window) {
|
||||
std::cerr << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
|
||||
SDL_Quit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
||||
if (!renderer) {
|
||||
std::cerr << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
SDL_Event e;
|
||||
bool quit = false;
|
||||
int isFullscreen = 0;
|
||||
|
||||
while (!quit) {
|
||||
while (SDL_PollEvent(&e)) {
|
||||
if (e.type == SDL_QUIT) {
|
||||
quit = true;
|
||||
} else if (e.type == SDL_MOUSEMOTION) {
|
||||
// Získání souřadnic myši
|
||||
int mouseX = e.motion.x, mouseY = e.motion.y;
|
||||
|
||||
// Přepočet souřadnic na poměr vůči rozměrům okna
|
||||
int windowWidth, windowHeight;
|
||||
SDL_GetWindowSize(window, &windowWidth, &windowHeight);
|
||||
|
||||
float normalizedX = (float)mouseX / windowWidth;
|
||||
float normalizedY = (float)mouseY / windowHeight;
|
||||
|
||||
std::cout << "Mouse moved to: (" << mouseX << ", " << mouseY << ") -> Normalized: ("
|
||||
<< normalizedX << ", " << normalizedY << ")" << std::endl;
|
||||
} else if (e.type == SDL_MOUSEBUTTONDOWN || e.type == SDL_MOUSEBUTTONUP) {
|
||||
int mouseX = e.button.x, mouseY = e.button.y;
|
||||
int button = e.button.button;
|
||||
|
||||
if (e.type == SDL_MOUSEBUTTONDOWN) {
|
||||
std::cout << "Mouse button pressed at: (" << mouseX << ", " << mouseY << ") button " << button << std::endl;
|
||||
} else {
|
||||
std::cout << "Mouse button released at: (" << mouseX << ", " << mouseY << ") button " << button << std::endl;
|
||||
}
|
||||
} else if (e.type == SDL_MOUSEWHEEL) {
|
||||
if (e.wheel.y > 0) {
|
||||
std::cout << "Mouse wheel scrolled up." << std::endl;
|
||||
} else if (e.wheel.y < 0) {
|
||||
std::cout << "Mouse wheel scrolled down." << std::endl;
|
||||
}
|
||||
} else if (e.type == SDL_KEYDOWN) {
|
||||
if (e.key.keysym.sym == SDLK_RETURN && (e.key.keysym.mod & KMOD_ALT)) {
|
||||
// Přepnutí mezi fullscreen a oknem
|
||||
isFullscreen = !isFullscreen;
|
||||
SDL_SetWindowFullscreen(window, isFullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Renderování a další logika hry
|
||||
SDL_RenderClear(renderer);
|
||||
SDL_RenderPresent(renderer);
|
||||
}
|
||||
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
return 0;
|
||||
}
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
#define WIDTH 640
|
||||
#define HEIGHT 480
|
||||
|
||||
#define PITCH 1024 // Pro optimalizaci paměti (pitch může být širší než WIDTH)
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
// Inicializace SDL
|
||||
|
@ -14,14 +14,14 @@ int main(int argc, char *argv[]) {
|
|||
}
|
||||
|
||||
// Vytvoření SDL okna
|
||||
SDL_Window *window = SDL_CreateWindow("DOS Game Port", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT, 0);
|
||||
SDL_Window *window = SDL_CreateWindow("DOS Game Port", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT, SDL_WINDOW_RESIZABLE);
|
||||
if (!window) {
|
||||
fprintf(stderr, "Chyba při vytváření okna: %s\n", SDL_GetError());
|
||||
SDL_Quit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Vytvoření rendereru (pouze pro zobrazení)
|
||||
// Vytvoření SDL rendereru
|
||||
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
||||
if (!renderer) {
|
||||
fprintf(stderr, "Chyba při vytváření rendereru: %s\n", SDL_GetError());
|
||||
|
@ -30,9 +30,9 @@ int main(int argc, char *argv[]) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
// Vytvoření SDL Surface (backbuffer)
|
||||
SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB565,SDL_TEXTUREACCESS_STREAMING, WIDTH, HEIGHT);
|
||||
if (!texture) {
|
||||
// Vytvoření softwarového backbufferu (surface)
|
||||
SDL_Surface *backbuffer = SDL_CreateRGBSurfaceWithFormat(0, PITCH, HEIGHT, 16, SDL_PIXELFORMAT_RGB565);
|
||||
if (!backbuffer) {
|
||||
fprintf(stderr, "Chyba při vytváření surface: %s\n", SDL_GetError());
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
|
@ -40,31 +40,56 @@ int main(int argc, char *argv[]) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
void *raw_pixels;
|
||||
int pitch;
|
||||
SDL_LockTexture(texture, NULL, &raw_pixels, &pitch);
|
||||
// Přímý přístup do paměti surface
|
||||
Uint16 *pixels = reinterpret_cast<Uint16 *>(raw_pixels);
|
||||
printf("Adresa pixelů: %p, pitch: %d bajtů\n", (void *)pixels, pitch);
|
||||
|
||||
// Software rendering - příklad kreslení do paměti
|
||||
for (int y = 0; y < HEIGHT; y++) {
|
||||
for (int x = 0; x < WIDTH; x++) {
|
||||
Uint16 color = (x ^ y) & 0xFFFF; // Příklad barvy
|
||||
pixels[y * (pitch / 2) + x] = color; // Zápis pixelu
|
||||
}
|
||||
// Vytvoření textury pro zobrazení backbufferu
|
||||
SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB565, SDL_TEXTUREACCESS_STREAMING, WIDTH, HEIGHT);
|
||||
if (!texture) {
|
||||
fprintf(stderr, "Chyba při vytváření textury: %s\n", SDL_GetError());
|
||||
SDL_FreeSurface(backbuffer);
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
SDL_UnlockTexture(texture);
|
||||
// Hlavní smyčka
|
||||
int running = 1;
|
||||
int frame = 0;
|
||||
while (running) {
|
||||
frame++;
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type == SDL_QUIT) {
|
||||
running = 0;
|
||||
}
|
||||
}
|
||||
|
||||
SDL_RenderClear(renderer);
|
||||
SDL_RenderCopy(renderer, texture, NULL, NULL);
|
||||
SDL_RenderPresent(renderer);
|
||||
// Přímý přístup do paměti backbufferu
|
||||
Uint16 *pixels = (Uint16 *)backbuffer->pixels;
|
||||
int pitch = backbuffer->pitch / 2; // Pitch v počtu pixelů (ne bajtů)
|
||||
|
||||
SDL_Delay(3000); // Zobrazení okna na 3 sekundy
|
||||
// Software rendering - příklad (čtení a zápis do backbufferu)
|
||||
for (int y = 0; y < HEIGHT; y++) {
|
||||
for (int x = 0; x < WIDTH; x++) {
|
||||
Uint16 color = (x ^ y ^ frame) & 0xFFFF; // Vzor barvy (testovací)
|
||||
pixels[y * pitch + x] = color; // Zápis pixelu
|
||||
}
|
||||
}
|
||||
|
||||
// Kopírování backbufferu do textury
|
||||
SDL_UpdateTexture(texture, NULL, backbuffer->pixels, backbuffer->pitch);
|
||||
|
||||
// Vykreslení textury na obrazovku
|
||||
SDL_RenderClear(renderer);
|
||||
SDL_RenderCopy(renderer, texture, NULL, NULL);
|
||||
SDL_RenderPresent(renderer);
|
||||
|
||||
// Zpoždění pro demonstraci (30 FPS)
|
||||
SDL_Delay(1000 / 30);
|
||||
}
|
||||
|
||||
// Uvolnění zdrojů
|
||||
SDL_DestroyTexture(texture);
|
||||
SDL_FreeSurface(backbuffer);
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
|
|
91
platform/sdl/tests/sdl_update_texture.cpp
Normal file
91
platform/sdl/tests/sdl_update_texture.cpp
Normal file
|
@ -0,0 +1,91 @@
|
|||
#include <SDL2/SDL.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define WIDTH 640
|
||||
#define HEIGHT 480
|
||||
#define PITCH 1024 // Pro optimalizaci paměti (pitch může být širší než WIDTH)
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
// Inicializace SDL
|
||||
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
|
||||
fprintf(stderr, "Chyba při inicializaci SDL: %s\n", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Vytvoření SDL okna
|
||||
SDL_Window *window = SDL_CreateWindow("DOS Game Port", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT, SDL_WINDOW_RESIZABLE);
|
||||
if (!window) {
|
||||
fprintf(stderr, "Chyba při vytváření okna: %s\n", SDL_GetError());
|
||||
SDL_Quit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
// Vytvoření SDL rendereru
|
||||
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
||||
if (!renderer) {
|
||||
fprintf(stderr, "Chyba při vytváření rendereru: %s\n", SDL_GetError());
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
// Vytvoření textury pro zobrazení backbufferu
|
||||
SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB565, SDL_TEXTUREACCESS_STREAMING, WIDTH, HEIGHT);
|
||||
if (!texture) {
|
||||
fprintf(stderr, "Chyba při vytváření textury: %s\n", SDL_GetError());
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint16_t *pixels = new uint16_t[HEIGHT*WIDTH];
|
||||
int pitch = WIDTH;
|
||||
|
||||
|
||||
// Hlavní smyčka
|
||||
int running = 1;
|
||||
int frame = 0;
|
||||
while (running) {
|
||||
frame++;
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type == SDL_QUIT) {
|
||||
running = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Přímý přístup do paměti backbufferu
|
||||
|
||||
// Software rendering - příklad (čtení a zápis do backbufferu)
|
||||
for (int y = 0; y < HEIGHT; y++) {
|
||||
for (int x = 0; x < WIDTH; x++) {
|
||||
Uint16 color = (x ^ y ^ frame) & 0xFFFF; // Vzor barvy (testovací)
|
||||
pixels[y * pitch + x] = color; // Zápis pixelu
|
||||
}
|
||||
}
|
||||
|
||||
// Kopírování backbufferu do textury
|
||||
SDL_UpdateTexture(texture, NULL, pixels, pitch*2);
|
||||
|
||||
// Vykreslení textury na obrazovku
|
||||
SDL_RenderClear(renderer);
|
||||
SDL_RenderCopy(renderer, texture, NULL, NULL);
|
||||
SDL_RenderPresent(renderer);
|
||||
|
||||
// Zpoždění pro demonstraci (30 FPS)
|
||||
SDL_Delay(1000 / 30);
|
||||
}
|
||||
|
||||
// Uvolnění zdrojů
|
||||
SDL_DestroyTexture(texture);
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
delete [] pixels;
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue