From e1518cf76fdca1482b093f3c0d8a40a06a0df2c1 Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Mon, 12 Feb 2024 14:23:21 +0100 Subject: [PATCH] - Canvis en el format de la paleta i en el volcat a textura de SDL. --- jdraw8.cpp | 22 ++++++++++++---------- jdraw8.h | 9 +++++---- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/jdraw8.cpp b/jdraw8.cpp index 8e77a91..e7dc08e 100644 --- a/jdraw8.cpp +++ b/jdraw8.cpp @@ -27,15 +27,15 @@ SDL_Renderer* sdlRenderer = NULL; SDL_Texture* sdlTexture = NULL; void JD8_Init(const char *title) { - screen = (JD8_Surface)calloc( 1, 64000 ); - main_palette = (JD8_Palette)calloc( 1, 768 ); + screen = (JD8_Surface)calloc( 1, 320 * 200 ); + main_palette = (JD8_Palette)calloc( 1, 256 * sizeof(uint32_t) ); pixel_data = (Uint32*)calloc(1, 320 * 200 * 4); // 1048576 ); sdlWindow = SDL_CreateWindow( title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN ); sdlRenderer = SDL_CreateRenderer(sdlWindow, -1, 0); SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "nearest"); - sdlTexture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, 320, 200); + sdlTexture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 320, 200); } void JD8_Quit() { @@ -82,8 +82,15 @@ JD8_Palette JD8_LoadPalette(const char *file) { char *buffer = NULL; buffer = JF_GetBufferFromResource(file, filesize); - JD8_Palette palette = (JD8_Palette)LoadPalette((unsigned char*)buffer); + uint8_t *pal = LoadPalette((unsigned char*)buffer); + JD8_Palette palette = (JD8_Palette)malloc(256 * sizeof(uint32_t)); + for (int i=0; i<256; ++i) { + palette[i].r = pal[i*3]; + palette[i].g = pal[1 + i*3]; + palette[i].b = pal[2 + i*3]; + //palette[i].hex = 0xff0000; + } return palette; } @@ -173,12 +180,7 @@ void JD8_BlitCKToSurface(int x, int y, JD8_Surface surface, int sx, int sy, int SDL_Rect rect{0, 0, SCREEN_WIDTH, SCREEN_HEIGHT}; void JD8_Flip() { - for( int x = 0; x < 320; x++ ) { - for( int y = 0; y < 200; y++ ) { - Uint32 color = 0xFF000000 + main_palette[screen[x + ( y * 320 )]].r + ( main_palette[screen[x + ( y * 320 )]].g << 8 ) + ( main_palette[screen[x + ( y * 320 )]].b << 16 ); - pixel_data[x + ( y * 320 )] = color; - } - } + for (int i=0; i<64000; ++i) pixel_data[i] = main_palette[screen[i]].hex; SDL_UpdateTexture(sdlTexture, NULL, pixel_data, 320 * sizeof(Uint32)); SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, &rect); SDL_RenderPresent(sdlRenderer); diff --git a/jdraw8.h b/jdraw8.h index 5045d29..7221f95 100644 --- a/jdraw8.h +++ b/jdraw8.h @@ -1,10 +1,11 @@ #pragma once #include -struct Color { - Uint8 r; - Uint8 g; - Uint8 b; +union Color { + struct { + uint8_t b, g, r, a; + }; + uint32_t hex; }; typedef Uint8* JD8_Surface;