1
0

en proces de pasar a SDL3

This commit is contained in:
2025-03-24 20:02:28 +01:00
parent 0334a7e04e
commit 7abbaee706
12 changed files with 216 additions and 40 deletions

105
.gitignore vendored
View File

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

49
CMakeLists.txt Normal file
View File

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

View File

@@ -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
# Regla para limpiar archivos generados
clean:
rm -f $(EXECUTABLE_NAME)$(OUTPUT_EXT)

3
README.md Normal file
View File

@@ -0,0 +1,3 @@
# demo6_palette
Demo de SDL que mostra com canviar la paleta dels sprites

View File

Before

Width:  |  Height:  |  Size: 273 B

After

Width:  |  Height:  |  Size: 273 B

View File

Before

Width:  |  Height:  |  Size: 273 B

After

Width:  |  Height:  |  Size: 273 B

View File

Before

Width:  |  Height:  |  Size: 1.7 KiB

After

Width:  |  Height:  |  Size: 1.7 KiB

View File

@@ -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);
}

View File

View File

@@ -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);
}
}
void jSetDest(jSurface surf)
{
if (surf == NULL)
jDestSurf = jScreen;
else
jDestSurf = surf;
jDestSurf = (surf == nullptr) ? jScreen : surf;
}
void jSetSource(jSurface surf)
@@ -53,21 +50,27 @@ 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));
}
}
}
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;
}

View File

@@ -1,5 +1,5 @@
#pragma once
#include <SDL2/SDL.h>
#include <SDL3/SDL.h>
typedef struct jSurface_s *jSurface;

View File

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