From 0249603e222c42eb344e32ef6f26e94a48fefa14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Valor=20Mart=C3=ADnez?= Date: Mon, 5 Feb 2024 19:18:47 +0100 Subject: [PATCH] Copiado el tutorial 5 de Jailgames Vintage en YouTube, pero algo sigue fallando --- Makefile | 2 +- game.cpp | 19 ++++++ jUnit.cpp | 163 +++++++++++++++++++++++++++++++++++++++++++++++++++ jUnit.h | 28 +++++++++ paleta.cpp | 36 ++---------- williams.gif | Bin 0 -> 1759 bytes 6 files changed, 215 insertions(+), 33 deletions(-) create mode 100644 game.cpp create mode 100644 jUnit.cpp create mode 100644 jUnit.h create mode 100644 williams.gif diff --git a/Makefile b/Makefile index ee5090c..497ddd3 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ name = paleta executable = $(name).o -source = $(name).cpp +source = *.cpp linux: g++ $(source) -std=c++11 -Wall -Os -lSDL2 -o "$(executable)" diff --git a/game.cpp b/game.cpp new file mode 100644 index 0000000..197dcc7 --- /dev/null +++ b/game.cpp @@ -0,0 +1,19 @@ +#include "jUnit.h" + +void init() +{ + jInit("pixels", 320, 240, 2); + jSetPal(0, 0x00000000); + jSetPal(1, 0xffff0000); + jSetPal(2, 0xffffffff); + jCls(0); + jSurface peiv = jLoadSurface("williams.gif"); + jLoadPal("williams.gif"); + jSetSource(peiv); +} + +void update() +{ + jCls(0); + jBlit(0, 0, 0, 0, 29, 64); +} \ No newline at end of file diff --git a/jUnit.cpp b/jUnit.cpp new file mode 100644 index 0000000..d0c31b6 --- /dev/null +++ b/jUnit.cpp @@ -0,0 +1,163 @@ +#include "jUnit.h" +#include "gif.c" +#include + +struct jSurface_s +{ + Uint8 *data; + Uint16 w, h; +}; + +static SDL_Window *jWin = NULL; +static SDL_Renderer *jRen = NULL; +static SDL_Texture *jTex = NULL; +static jSurface jScreen; +static jSurface jDestSurf; +static jSurface jSourceSurf = NULL; +static Uint32 paleta[256]; +static int jWidth = 320; +static int jHeight = 240; +static int jZoom = 2; + +jSurface jNewSurface(int w, int h) +{ + jSurface surf = (jSurface)malloc(sizeof(jSurface_s)); + surf->w = w; + surf->h = h; + surf->data = (Uint8 *)malloc(w * h); + return surf; +} + +void jDeleteSurface(jSurface surf) +{ + if (surf == NULL) + return; + if (surf->data != NULL) + free(surf->data); + free(surf); +} + +void jSetDest(jSurface surf) +{ + if (surf == NULL) + jDestSurf = jScreen; + else + jDestSurf = surf; +} + +void jSetSource(jSurface surf) +{ + jSourceSurf = surf; +} + +void jBlit(int dx, int dy, int sx, int sy, int w, int h) +{ + if (jSourceSurf == NULL) + 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; + + fseek(f, 0, SEEK_END); + long size = ftell(f); + fseek(f, 0, SEEK_SET); + Uint8 *buffer = (Uint8 *)malloc(size); + fread(buffer, size, 1, f); + fclose(f); + + Uint16 w, h; + Uint8 *pixels = LoadGif(buffer, &w, &h); + if (pixels == NULL) + { + return NULL; + } + jSurface surf = (jSurface)malloc(sizeof(jSurface_s)); + surf->w = w; + surf->h = h; + surf->data = pixels; + free(buffer); + return surf; +} + +void jLoadPal(const char *filename) +{ + FILE *f = fopen(filename, "rb"); + if (!f) + return; + + fseek(f, 0, SEEK_END); + long size = ftell(f); + fseek(f, 0, SEEK_SET); + Uint8 *buffer = (Uint8 *)malloc(size); + fread(buffer, size, 1, f); + fclose(f); + + Uint32 *pal = LoadPalette(buffer); + if (pal == NULL) + { + return; + } + free(buffer); + for (int i = 0; i < 256; ++i) + { + paleta[i] = pal[i]; + } +} + +void jInit(const char *titol, int w, int h, int z) +{ + SDL_Init(SDL_INIT_EVERYTHING); + 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); + jTex = SDL_CreateTexture(jRen, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, w, h); + jScreen = jNewSurface(w, h); + jDestSurf = jScreen; +} + +void jSetPal(int index, Uint32 color) +{ + paleta[index] = 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); + for (int i = 0; i < jWidth * jHeight; ++i) + pixels[i] = paleta[jScreen->data[i]]; + SDL_UnlockTexture(jTex); + SDL_RenderCopy(jRen, jTex, NULL, NULL); + SDL_RenderPresent(jRen); +} + +void jPutPixel(int x, int y, Uint8 color) +{ + if (x < 0 || y < 0 || x >= jDestSurf->w || y >= jDestSurf->h) + return; + jDestSurf->data[x + y * jDestSurf->w] = color; +} + +Uint8 jGetPixel(int x, int y) +{ + return jDestSurf->data[x + y * jDestSurf->w]; +} \ No newline at end of file diff --git a/jUnit.h b/jUnit.h new file mode 100644 index 0000000..2f63387 --- /dev/null +++ b/jUnit.h @@ -0,0 +1,28 @@ +#pragma once +#include + +typedef struct jSurface_s *jSurface; + +void init(); +void update(); + +jSurface jNewSurface(int w, int h); +void jDeleteSurface(jSurface surf); +void jSetDest(jSurface surf); +void jSetSource(jSurface surf); + +jSurface jLoadSurface(const char* filename); + +void jPutPixel(int x, int y, Uint8 color); +Uint8 jGetPixel(int x, int y); + +void jBlit(int dx, int dy, int sx, int sy, int w, int h); + +void jInit(const char *titol, int w, int h, int z); + +void jSetPal(int index, Uint32 color); +void jLoadPal(const char *filename); + +void jCls(Uint8 color); + +void jFlip(); diff --git a/paleta.cpp b/paleta.cpp index ecd28e6..c31b102 100644 --- a/paleta.cpp +++ b/paleta.cpp @@ -1,22 +1,8 @@ -#include - -struct ARGB -{ - Uint8 b, g, r, a; -}; +#include "jUnit.h" int main(int argc, char *argv[]) { - SDL_Init(SDL_INIT_EVERYTHING); - - SDL_Window *w = SDL_CreateWindow("pixels", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN); - SDL_Renderer *r = SDL_CreateRenderer(w, -1, 0); - SDL_RenderSetLogicalSize(r, 320, 240); - SDL_Texture *t = SDL_CreateTexture(r, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 320, 240); - - ARGB *pixels; - int pitch; - + init(); SDL_Event sdlEvent; bool exit = false; while (!exit) @@ -28,22 +14,8 @@ int main(int argc, char *argv[]) exit = true; break; } + update(); + jFlip(); } - - SDL_LockTexture(t, nullptr, (void **)&pixels, &pitch); - - for (int i = 0; i < 76800; ++i) - { - pixels[i].b = rand() % 256; - pixels[i].g = rand() % 256; - pixels[i].r = rand() % 256; - pixels[i].a = 255; - } - - SDL_UnlockTexture(t); - - SDL_RenderCopy(r, t, nullptr, nullptr); - - SDL_RenderPresent(r); } } \ No newline at end of file diff --git a/williams.gif b/williams.gif new file mode 100644 index 0000000000000000000000000000000000000000..421cb9055e8a470975a725f60b71aa6266d42312 GIT binary patch literal 1759 zcmV<51|a!INk%v~VRQgM0CxZYA^!_bMO0HmK~P09E-(WD0000X`2-0H0000i00000 zbO1mAg8%>kA^-p)A|fI)GiD+pGgW3pRYYcYcZftps%B>0cX#fYi2u8)yQ<9p|Nj60 z00jRK$VsccIP1;3{~00%Bxs(1h>!v5a%z%{Z#tH5Jg>FN<_Q8xfe_|IG8zgI92Nrz zmri*iJcEhfi75;ag;7)SO332GP49E55Gu@p;MOX{YC(v$7cQ@o;Z-CA3JY%u3N2!K zZ!CQvU4w-(h+B(%1Sd@i3k(Yhor#ZvgoczRnw*}XfdP@Fp&yV?3`A!isGXk+upY7q zv@5(FxT?E;aR+e>aVWmPw!JF}$)C)u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