From 7abbaee7061dc6ab349dd6fe0a2428cba2c8d4d8 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Mon, 24 Mar 2025 20:02:28 +0100 Subject: [PATCH] en proces de pasar a SDL3 --- .gitignore | 105 +++++++++++++++++++++++++ CMakeLists.txt | 49 ++++++++++++ Makefile | 26 +++--- README.md | 3 + pal01.gif => resources/pal01.gif | Bin pal02.gif => resources/pal02.gif | Bin williams.gif => resources/williams.gif | Bin game.cpp => source/game.cpp | 6 +- gif.c => source/gif.c | 0 jUnit.cpp => source/jUnit.cpp | 59 ++++++++------ jUnit.h => source/jUnit.h | 2 +- paleta.cpp => source/paleta.cpp | 6 +- 12 files changed, 216 insertions(+), 40 deletions(-) create mode 100644 CMakeLists.txt create mode 100644 README.md rename pal01.gif => resources/pal01.gif (100%) rename pal02.gif => resources/pal02.gif (100%) rename williams.gif => resources/williams.gif (100%) rename game.cpp => source/game.cpp (54%) rename gif.c => source/gif.c (100%) rename jUnit.cpp => source/jUnit.cpp (77%) rename jUnit.h => source/jUnit.h (96%) rename paleta.cpp => source/paleta.cpp (56%) diff --git a/.gitignore b/.gitignore index 25a7384..b407fdd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,107 @@ +demo6_palette +build/ + +# ---> C++ +# Prerequisites +*.d + +# Compiled Object files +*.slo +*.lo *.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables *.exe +*.out +*.app + +# ---> macOS +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +# ---> Windows +# Windows thumbnail cache files +Thumbs.db +Thumbs.db:encryptable +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +# ---> Linux +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..620ac3c --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,49 @@ +cmake_minimum_required(VERSION 3.20) +project(demo6_palette) + +# Establecer el estándar de C++ +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +# Opciones comunes de compilación +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Os -ffunction-sections -fdata-sections") + +# Buscar SDL3 automáticamente +find_package(SDL3 REQUIRED) + +# Si no se encuentra SDL3, generar un error +if (NOT SDL3_FOUND) + message(FATAL_ERROR "SDL3 no encontrado. Por favor, verifica su instalación.") +endif() + +# Archivos fuente +file(GLOB SOURCE_FILES source/*.cpp) + +# Comprobar si se encontraron archivos fuente +if(NOT SOURCE_FILES) + message(FATAL_ERROR "No se encontraron archivos fuente en el directorio 'source/'. Verifica la ruta.") +endif() + +# Detectar la plataforma y configuraciones específicas +if(WIN32) + set(PLATFORM windows) + set(LINK_LIBS ${SDL3_LIBRARIES} mingw32 ws2_32) +elseif(UNIX AND NOT APPLE) + set(PLATFORM linux) + set(LINK_LIBS ${SDL3_LIBRARIES}) +elseif(APPLE) + set(PLATFORM macos) + set(LINK_LIBS ${SDL3_LIBRARIES}) +endif() + +# Incluir directorios de SDL3 +include_directories(${SDL3_INCLUDE_DIRS}) + +# Añadir el ejecutable reutilizando el nombre del proyecto +add_executable(${PROJECT_NAME} ${SOURCE_FILES}) + +# Especificar la ubicación del ejecutable (en la raíz del proyecto) +set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}) + +# Enlazar las bibliotecas necesarias +target_link_libraries(${PROJECT_NAME} ${LINK_LIBS}) diff --git a/Makefile b/Makefile index 53f2982..f78269e 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,20 @@ -name = paleta -executable = $(name) -source = *.cpp +# Variables comunes +SOURCE := source/*.cpp +EXECUTABLE_NAME := demo6_palette +CXXFLAGS := -std=c++20 -Wall -Os -ffunction-sections -fdata-sections # Opciones comunes de compilación +LDFLAGS := -lSDL3 # Flags de enlace comunes +OUTPUT_EXT := -windows: - g++ $(source) -std=c++11 -Wall -lmingw32 -lSDL2main -lSDL2 -mwindows -o $(executable).exe +# Detectar plataforma y configurar +ifeq ($(OS),Windows_NT) + LDFLAGS += -lmingw32 -lws2_32 + OUTPUT_EXT := .exe +endif -linux: - g++ $(source) -std=c++11 -Wall -lSDL2 -o $(executable).o +# Regla principal: compilar el ejecutable +all: + $(CXX) $(SOURCE) $(CXXFLAGS) $(LDFLAGS) -o $(EXECUTABLE_NAME)$(OUTPUT_EXT) -macos: - clang++ $(source) -std=c++11 -Wall -lSDL2 -ffunction-sections -fdata-sections -o $(executable).o \ No newline at end of file +# Regla para limpiar archivos generados +clean: + rm -f $(EXECUTABLE_NAME)$(OUTPUT_EXT) \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..493c408 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# demo6_palette + +Demo de SDL que mostra com canviar la paleta dels sprites \ No newline at end of file diff --git a/pal01.gif b/resources/pal01.gif similarity index 100% rename from pal01.gif rename to resources/pal01.gif diff --git a/pal02.gif b/resources/pal02.gif similarity index 100% rename from pal02.gif rename to resources/pal02.gif diff --git a/williams.gif b/resources/williams.gif similarity index 100% rename from williams.gif rename to resources/williams.gif diff --git a/game.cpp b/source/game.cpp similarity index 54% rename from game.cpp rename to source/game.cpp index da9ef67..28ad512 100644 --- a/game.cpp +++ b/source/game.cpp @@ -2,11 +2,11 @@ void init() { - jInit("tutorial_paleta", 320, 240, 2); + jInit("demo6_palette", 320, 240, 2); jSetPal(0, 0x00000000); jCls(0); - jSurface peiv = jLoadSurface("williams.gif"); - jLoadPal("pal01.gif"); + jSurface peiv = jLoadSurface("resources/williams.gif"); + jLoadPal("resources/pal01.gif"); jSetSource(peiv); } diff --git a/gif.c b/source/gif.c similarity index 100% rename from gif.c rename to source/gif.c diff --git a/jUnit.cpp b/source/jUnit.cpp similarity index 77% rename from jUnit.cpp rename to source/jUnit.cpp index 9d0f43d..4f4902b 100644 --- a/jUnit.cpp +++ b/source/jUnit.cpp @@ -8,12 +8,12 @@ struct jSurface_s Uint16 w, h; }; -static SDL_Window *jWin = NULL; -static SDL_Renderer *jRen = NULL; -static SDL_Texture *jTex = NULL; +static SDL_Window *jWin = nullptr; +static SDL_Renderer *jRen = nullptr; +static SDL_Texture *jTex = nullptr; static jSurface jScreen; static jSurface jDestSurf; -static jSurface jSourceSurf = NULL; +static jSurface jSourceSurf = nullptr; static Uint32 paleta[256]; static int jWidth = 320; static int jHeight = 240; @@ -31,19 +31,16 @@ jSurface jNewSurface(int w, int h) void jDeleteSurface(jSurface surf) { - if (surf == NULL) - return; - if (surf->data != NULL) + if (surf) + { free(surf->data); - free(surf); + free(surf); + } } void jSetDest(jSurface surf) { - if (surf == NULL) - jDestSurf = jScreen; - else - jDestSurf = surf; + jDestSurf = (surf == nullptr) ? jScreen : surf; } void jSetSource(jSurface surf) @@ -53,13 +50,17 @@ void jSetSource(jSurface surf) void jBlit(int dx, int dy, int sx, int sy, int w, int h) { - if (jSourceSurf == NULL) + if (jSourceSurf == nullptr) + { return; - + } + for (int iy = 0; iy < h; ++iy) { for (int ix = 0; ix < w; ++ix) + { jPutPixel(dx + ix, dy + iy, jGetPixel(sx + ix, sy + iy)); + } } } @@ -67,7 +68,9 @@ jSurface jLoadSurface(const char *filename) { FILE *f = fopen(filename, "rb"); if (!f) - return NULL; + { + return nullptr; + } fseek(f, 0, SEEK_END); long size = ftell(f); @@ -78,9 +81,9 @@ jSurface jLoadSurface(const char *filename) Uint16 w, h; Uint8 *pixels = LoadGif(buffer, &w, &h); - if (pixels == NULL) + if (pixels == nullptr) { - return NULL; + return nullptr; } jSurface surf = (jSurface)malloc(sizeof(jSurface_s)); surf->w = w; @@ -94,7 +97,9 @@ void jLoadPal(const char *filename) { FILE *f = fopen(filename, "rb"); if (!f) + { return; + } fseek(f, 0, SEEK_END); long size = ftell(f); @@ -104,7 +109,7 @@ void jLoadPal(const char *filename) fclose(f); Uint32 *pal = LoadPalette(buffer); - if (pal == NULL) + if (pal == nullptr) { return; } @@ -117,13 +122,13 @@ void jLoadPal(const char *filename) void jInit(const char *titol, int w, int h, int z) { - SDL_Init(SDL_INIT_EVERYTHING); + SDL_Init(SDL_INIT_VIDEO); jWidth = w; jHeight = h; jZoom = z; - jWin = SDL_CreateWindow(titol, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w * z, h * z, SDL_WINDOW_SHOWN); - jRen = SDL_CreateRenderer(jWin, -1, 0); - SDL_RenderSetLogicalSize(jRen, w, h); + jWin = SDL_CreateWindow(titol, w * z, h * z, 0); + jRen = SDL_CreateRenderer(jWin, nullptr); + SDL_SetRenderLogicalPresentation(jRen, w, h, SDL_LOGICAL_PRESENTATION_INTEGER_SCALE); jTex = SDL_CreateTexture(jRen, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, w, h); jScreen = jNewSurface(w, h); jDestSurf = jScreen; @@ -137,25 +142,31 @@ void jSetPal(int index, Uint32 color) void jCls(Uint8 color) { for (int i = 0; i < jDestSurf->w * jDestSurf->h; ++i) + { jDestSurf->data[i] = color; + } } void jFlip() { Uint32 *pixels; int pitch; - SDL_LockTexture(jTex, NULL, (void **)&pixels, &pitch); + SDL_LockTexture(jTex, nullptr, (void **)&pixels, &pitch); for (int i = 0; i < jWidth * jHeight; ++i) + { pixels[i] = paleta[jScreen->data[i]]; + } SDL_UnlockTexture(jTex); - SDL_RenderCopy(jRen, jTex, NULL, NULL); + SDL_RenderTexture(jRen, jTex, nullptr, nullptr); SDL_RenderPresent(jRen); } void jPutPixel(int x, int y, Uint8 color) { if (x < 0 || y < 0 || x >= jDestSurf->w || y >= jDestSurf->h || color == transparentColor) + { return; + } jDestSurf->data[x + y * jDestSurf->w] = color; } diff --git a/jUnit.h b/source/jUnit.h similarity index 96% rename from jUnit.h rename to source/jUnit.h index 2f63387..a375dbd 100644 --- a/jUnit.h +++ b/source/jUnit.h @@ -1,5 +1,5 @@ #pragma once -#include +#include typedef struct jSurface_s *jSurface; diff --git a/paleta.cpp b/source/paleta.cpp similarity index 56% rename from paleta.cpp rename to source/paleta.cpp index c31b102..0a8c774 100644 --- a/paleta.cpp +++ b/source/paleta.cpp @@ -3,13 +3,13 @@ int main(int argc, char *argv[]) { init(); - SDL_Event sdlEvent; + SDL_Event event; bool exit = false; while (!exit) { - while (SDL_PollEvent(&sdlEvent)) + while (SDL_PollEvent(&event)) { - if (sdlEvent.type == SDL_QUIT) + if ((event.type == SDL_EVENT_QUIT) || (event.type == SDL_EVENT_KEY_DOWN && event.key.repeat == 0 && event.key.key == SDLK_ESCAPE)) { exit = true; break;