diff options
author | kdx <kikoodx@paranoici.org> | 2023-05-02 23:54:43 +0200 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2023-05-02 23:54:46 +0200 |
commit | 62b3fdad21f3dfb03aa2e4eb5173a1dac68cd518 (patch) | |
tree | 2fd0a3114d1b6023c8ab2281e4da1312746d1826 | |
parent | 90ece2581ea402b960543f470c7972e1bd329e6d (diff) | |
download | px-62b3fdad21f3dfb03aa2e4eb5173a1dac68cd518.tar.gz |
drasticaly optimize flipping
-rw-r--r-- | src/main.c | 8 | ||||
-rw-r--r-- | src/px.c | 39 | ||||
-rw-r--r-- | src/px.h | 7 |
3 files changed, 50 insertions, 4 deletions
@@ -6,12 +6,18 @@ int main(void) { - if (TZR_Init(128, 128, 60, true, false, false, false, false, "px")) + if (TZR_Init(128, 128, 60, .mixer=false)) return 1; if (atexit(TZR_Quit)) { TZR_Quit(); return 1; } + if (pxInit()) + return 1; + if (atexit(pxDeinit)) { + pxDeinit(); + return 1; + } srand(time(NULL)); PxSpr spr = { @@ -29,20 +29,53 @@ static struct { int w; int h; } pxclip = {0, 0, PX_WIDTH, PX_HEIGHT}; +static SDL_Texture *pxtexture = NULL; + +int +pxInit(void) +{ + pxtexture = SDL_CreateTexture(___tzr_renderer, + SDL_PIXELFORMAT_ARGB8888, + SDL_TEXTUREACCESS_STREAMING, + PX_WIDTH, PX_HEIGHT); + if (pxtexture == NULL) { + fprintf(stderr, "%s\n", SDL_GetError()); + return 1; + } + return 0; +} + +void +pxDeinit(void) +{ + if (pxtexture != NULL) { + SDL_DestroyTexture(pxtexture); + pxtexture = NULL; + } +} void pxFlip(void) { + uint8_t *pixels = NULL; + int pitch = 0; + SDL_LockTexture(pxtexture, NULL, (void **)&pixels, &pitch); + for (int y = 0; y < PX_HEIGHT; y++) { for (int x = 0; x < PX_WIDTH; x++) { + const int ix = x << 2; PxCol col = pxbuf[x + y * PX_WIDTH]; if (pxpal[col].spal) col = pxpal[col].spal - 1; - TZR_DrawSetColor8(pxpal[col].r, pxpal[col].g, - pxpal[col].b, 255); - TZR_DrawPoint(x, y); + pixels[ix + y * pitch + 0] = pxpal[col].b; + pixels[ix + y * pitch + 1] = pxpal[col].g; + pixels[ix + y * pitch + 2] = pxpal[col].r; + pixels[ix + y * pitch + 3] = 255; } } + + SDL_UnlockTexture(pxtexture); + SDL_RenderCopy(___tzr_renderer, pxtexture, NULL, NULL); } void @@ -50,6 +50,13 @@ typedef struct { extern PxCol pxbuf[PX_WIDTH * PX_HEIGHT]; extern PxPal pxpal[256]; +/* Return -1 on error. Should be called after TZR_Init. */ +int pxInit(void); + +/* Should be called before TZR_Quit. */ +void pxDeinit(void); + +/* Should be called between TZR_DrawBegin and TZR_DrawEnd. */ void pxFlip(void); /*** DRAW ***/ |