From c1e5caa724e1e7bc9996cd94407df6538e96eb72 Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Tue, 21 Nov 2023 18:44:09 +0100 Subject: [PATCH] - First commit --- .gitignore | 2 + Makefile | 2 + main.c | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..448b545 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +jix +*.exe diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..40239a3 --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +all: + gcc main.c -lmingw32 -lSDL2main -lSDL2 -o jix.exe diff --git a/main.c b/main.c new file mode 100644 index 0000000..59cf88a --- /dev/null +++ b/main.c @@ -0,0 +1,120 @@ +#include +#include + +#define AMUNT 0 +#define DRETA 1 +#define AVALL 2 +#define ESQUERRA 3 + +#define OFFSET_X 60 +#define OFFSET_Y 35 + +#define MODE_NORMAL 0 +#define MODE_CREANT 1 + +SDL_Window *win; +SDL_Renderer *ren; +SDL_Texture * tex; + +Uint8 pixels[200*200]; +int player_x = 100; +int player_y = 199; +int mode = MODE_NORMAL; + +void draw_player() +{ + const int x = player_x+OFFSET_X; + const int y = player_y+OFFSET_Y; + SDL_SetRenderDrawColor(ren, 217, 87, 99, 255); + SDL_RenderDrawLine(ren, x, y-3, x+3, y); + SDL_RenderDrawLine(ren, x+3, y, x, y+3); + SDL_RenderDrawLine(ren, x, y+3, x-3, y); + SDL_RenderDrawLine(ren, x-3, y, x, y-3); +} + +void put_pix() +{ + pixels[player_x+player_y*200] = 2; + SDL_SetRenderTarget(ren, tex); + SDL_SetRenderDrawColor(ren, 128, 128, 128, 255); + SDL_RenderDrawPoint(ren, player_x, player_y); + SDL_SetRenderTarget(ren, NULL); +} + +void close_sector() +{ + SDL_SetRenderTarget(ren, tex); + SDL_SetRenderDrawColor(ren, 255, 255, 255, 255); + for (int y=0;y<200;++y) + for (int x=0;x<200;++x) + if (pixels[x+y*200]==2) + { + pixels[x+y*200]=1; + SDL_RenderDrawPoint(ren, x, y); + } + SDL_SetRenderTarget(ren, NULL); +} + +int main(int argc, char *argv[]) +{ + srand(SDL_GetTicks()); + SDL_Init(SDL_INIT_EVERYTHING); + win = SDL_CreateWindow("JIX", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 960, 720, SDL_WINDOW_SHOWN); + ren = SDL_CreateRenderer(win, -1, 0); + tex = SDL_CreateTexture(ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, 200, 200); + SDL_RenderSetLogicalSize(ren, 320, 240); + + for (int i=0; i<200*200; ++i) pixels[i]=0; + for (int i=0; i<200; ++i) pixels[i]=pixels[i+199*200]=pixels[i*200]=pixels[199+i*200]=1; + + SDL_SetRenderTarget(ren, tex); + SDL_SetRenderDrawColor(ren, 0, 0, 0, 255); + SDL_RenderClear(ren); + SDL_SetRenderDrawColor(ren, 255, 255, 255, 255); + SDL_RenderDrawLine(ren, 0, 0, 199, 0); + SDL_RenderDrawLine(ren, 0, 0, 0, 199); + SDL_RenderDrawLine(ren, 199, 199, 199, 0); + SDL_RenderDrawLine(ren, 199, 199, 0, 199); + SDL_SetRenderTarget(ren, NULL); + + int should_exit = 0; + SDL_Event e; + Uint32 t = SDL_GetTicks(); + const Uint8 *keys = SDL_GetKeyboardState(NULL); + + while (!should_exit) + { + while (SDL_PollEvent(&e)) + { + if (e.type == SDL_QUIT) {should_exit=1;break;} + if (e.type == SDL_KEYDOWN) + { + switch(e.key.keysym.scancode) { + case SDL_SCANCODE_ESCAPE: should_exit = 1; break; + }; + } + } + + if (SDL_GetTicks()-t>=15) + { + if (keys[SDL_SCANCODE_UP] && ( mode==MODE_CREANT || keys[SDL_SCANCODE_SPACE] || (player_y>0 && pixels[player_x+(player_y-1)*200]==1))) { if (mode==MODE_CREANT) put_pix(); mode = MODE_CREANT; player_y--; } + else if (keys[SDL_SCANCODE_RIGHT] && ( mode==MODE_CREANT || keys[SDL_SCANCODE_SPACE] || (player_x<199 && pixels[player_x+1+player_y*200]==1))) { if (mode==MODE_CREANT) put_pix(); mode = MODE_CREANT; player_x++; } + else if (keys[SDL_SCANCODE_DOWN] && ( mode==MODE_CREANT || keys[SDL_SCANCODE_SPACE] || (player_y<199 && pixels[player_x+(player_y+1)*200]==1))) { if (mode==MODE_CREANT) put_pix(); mode = MODE_CREANT; player_y++; } + else if (keys[SDL_SCANCODE_LEFT] && ( mode==MODE_CREANT || keys[SDL_SCANCODE_SPACE] || (player_x>0 && pixels[player_x-1+player_y*200]==1))) { if (mode==MODE_CREANT) put_pix(); mode = MODE_CREANT; player_x--; } + if (pixels[player_x+player_y*200]==1) { + close_sector(); + mode = MODE_NORMAL; + } + t = SDL_GetTicks(); + } + SDL_SetRenderDrawColor(ren, 0, 0, 0, 255); + SDL_RenderClear(ren); + SDL_Rect rect = {OFFSET_X,OFFSET_Y,200,200}; + SDL_RenderCopy(ren, tex, NULL, &rect); + draw_player(); + SDL_RenderPresent(ren); + } + + SDL_Quit(); + return 0; +} \ No newline at end of file