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
|
@ -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