- Resolució i tamany de camp igual a la recreativa

- Logo
This commit is contained in:
2023-11-23 17:16:43 +01:00
parent 716f657278
commit 2d728e9121
2 changed files with 104 additions and 61 deletions

View File

@@ -1,5 +1,5 @@
windows: windows:
gcc main.c -lmingw32 -lSDL2main -lSDL2 -o jix.exe gcc -g main.c -lmingw32 -lSDL2main -lSDL2 -o jix.exe
linux: linux:
gcc main.c -lSDL2 -o jix gcc -g main.c -lSDL2 -o jix

161
main.c
View File

@@ -1,13 +1,20 @@
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <stdio.h> #include <stdio.h>
#define SCREEN_WIDTH 240
#define SCREEN_HEIGHT 256
#define SCREEN_ZOOM 3
#define CAMP_WIDTH 225
#define CAMP_HEIGHT 201
#define AMUNT 0 #define AMUNT 0
#define DRETA 1 #define DRETA 1
#define AVALL 2 #define AVALL 2
#define ESQUERRA 3 #define ESQUERRA 3
#define OFFSET_X 60 #define OFFSET_X 8
#define OFFSET_Y 35 #define OFFSET_Y 39
#define MODE_NORMAL 0 #define MODE_NORMAL 0
#define MODE_CREANT 1 #define MODE_CREANT 1
@@ -25,14 +32,14 @@ SDL_Renderer *ren;
SDL_Texture *tex; SDL_Texture *tex;
SDL_Texture *gfx; SDL_Texture *gfx;
Uint8 pixels[200*200]; Uint8 pixels[CAMP_WIDTH*CAMP_HEIGHT];
int player_x = 100; int player_x = CAMP_WIDTH/2;
int player_y = 198; int player_y = CAMP_HEIGHT-1;
int mode = MODE_NORMAL; int mode = MODE_NORMAL;
int lento = 0; int lento = 0;
int last_x = 100; int last_x = CAMP_WIDTH/2;
int last_y = 198; int last_y = CAMP_HEIGHT-1;
void set_color(int color) void set_color(int color)
{ {
@@ -47,6 +54,33 @@ void set_color(int color)
} }
} }
void set_color_mod(int color)
{
switch (color) {
case COLOR_BLACK: SDL_SetTextureColorMod(gfx, 0, 0, 0); break;
case COLOR_WHITE: SDL_SetTextureColorMod(gfx, 255, 255, 255); break;
case COLOR_GREY: SDL_SetTextureColorMod(gfx, 128, 128, 128); break;
case COLOR_RED: SDL_SetTextureColorMod(gfx, 217, 87, 99); break;
case COLOR_YELLOW: SDL_SetTextureColorMod(gfx, 251, 242, 54); break;
case COLOR_DARKRED: SDL_SetTextureColorMod(gfx, 172, 50, 50); break;
case COLOR_DARKTEAL: SDL_SetTextureColorMod(gfx, 48, 128, 130); break;
}
}
void print(char *text, int x, int y, int color, int grid, int offset)
{
const int len = strlen(text);
set_color_mod(color);
for (int i=0; i<len; ++i)
{
const int chr = text[i]+offset;
SDL_Rect src_rect = {8*(chr%16), 8*(chr/16), 8, 8};
SDL_Rect dest_rect = {x+grid*i, y, 8, 8};
SDL_RenderCopy(ren, gfx, &src_rect, &dest_rect);
}
//set_color_mod(COLOR_WHITE);
}
void draw_player() void draw_player()
{ {
const int x = player_x+OFFSET_X; const int x = player_x+OFFSET_X;
@@ -60,10 +94,10 @@ void draw_player()
void put_pix() void put_pix()
{ {
if (pixels[player_x+player_y*200]==COLOR_WHITE) return; if (pixels[player_x+player_y*CAMP_WIDTH]==COLOR_WHITE) return;
last_x = player_x; last_y = player_y; last_x = player_x; last_y = player_y;
pixels[player_x+player_y*200] = COLOR_DARKRED; pixels[player_x+player_y*CAMP_WIDTH] = COLOR_DARKRED;
SDL_SetRenderTarget(ren, tex); SDL_SetRenderTarget(ren, tex);
if (lento) if (lento)
set_color(COLOR_DARKRED); set_color(COLOR_DARKRED);
@@ -75,14 +109,14 @@ void put_pix()
int any_black(int x, int y) int any_black(int x, int y)
{ {
return (pixels[x+1+y*200]==0 || pixels[x-1+y*200]==0 || pixels[x+(y+1)*200]==0 || pixels[x+(y-1)*200]==0); return (pixels[x+1+y*CAMP_WIDTH]==0 || pixels[x-1+y*CAMP_WIDTH]==0 || pixels[x+(y+1)*CAMP_WIDTH]==0 || pixels[x+(y-1)*CAMP_WIDTH]==0);
} }
int get_sector_size(int x, int y, int size, int fill) int get_sector_size(int x, int y, int size, int fill)
{ {
if (x>=0 && y>=0 && x<=198 && y<=198 && pixels[x+y*200]==0) if (x>=0 && y>=0 && x<CAMP_WIDTH && y<CAMP_HEIGHT && pixels[x+y*CAMP_WIDTH]==0)
{ {
pixels[x+y*200]=fill; pixels[x+y*CAMP_WIDTH]=fill;
size++; size++;
size = get_sector_size(x-1, y, size, fill); size = get_sector_size(x-1, y, size, fill);
size = get_sector_size(x, y-1, size, fill); size = get_sector_size(x, y-1, size, fill);
@@ -96,35 +130,35 @@ void fill_sector(int fill)
{ {
const Uint8 color = lento ? COLOR_DARKRED : COLOR_DARKTEAL; const Uint8 color = lento ? COLOR_DARKRED : COLOR_DARKTEAL;
set_color(color); set_color(color);
for (int y=0;y<200;++y) for (int y=0;y<CAMP_HEIGHT;++y)
for (int x=0;x<200;++x) for (int x=0;x<CAMP_WIDTH;++x)
if (pixels[x+y*200]>=254) if (pixels[x+y*CAMP_WIDTH]>=254)
if (pixels[x+y*200]==fill) { if (pixels[x+y*CAMP_WIDTH]==fill) {
pixels[x+y*200]=color; pixels[x+y*CAMP_WIDTH]=color;
SDL_RenderDrawPoint(ren, x, y); SDL_RenderDrawPoint(ren, x, y);
} else { } else {
pixels[x+y*200]=0; pixels[x+y*CAMP_WIDTH]=0;
} }
} }
int get_percent() int get_percent()
{ {
int claimed = 0; int claimed = 0;
for (int y=0;y<200;++y) for (int y=0;y<CAMP_HEIGHT;++y)
for (int x=0;x<200;++x) for (int x=0;x<CAMP_WIDTH;++x)
if (pixels[x+y*200]!=0) claimed++; if (pixels[x+y*CAMP_WIDTH]!=0) claimed++;
return (claimed*100)/(200*200); return (claimed*100)/(CAMP_WIDTH*CAMP_HEIGHT);
} }
void close_sector() void close_sector()
{ {
SDL_SetRenderTarget(ren, tex); SDL_SetRenderTarget(ren, tex);
set_color(COLOR_WHITE); set_color(COLOR_WHITE);
for (int y=0;y<200;++y) for (int y=0;y<CAMP_HEIGHT;++y)
for (int x=0;x<200;++x) for (int x=0;x<CAMP_WIDTH;++x)
if ((pixels[x+y*200]==COLOR_DARKRED || pixels[x+y*200]==COLOR_DARKTEAL) && any_black(x,y)) if ((pixels[x+y*CAMP_WIDTH]==COLOR_DARKRED || pixels[x+y*CAMP_WIDTH]==COLOR_DARKTEAL) && any_black(x,y))
{ {
pixels[x+y*200]=COLOR_WHITE; pixels[x+y*CAMP_WIDTH]=COLOR_WHITE;
SDL_RenderDrawPoint(ren, x, y); SDL_RenderDrawPoint(ren, x, y);
} }
@@ -138,7 +172,8 @@ void close_sector()
if (sector2>sector1) fill_sector(254); else fill_sector(255); if (sector2>sector1) fill_sector(254); else fill_sector(255);
} }
printf("%i...\n", get_percent()); char result[20];
print(SDL_itoa(get_percent(), result, 10), 10, 10, COLOR_YELLOW, 8, 0);
SDL_SetRenderTarget(ren, NULL); SDL_SetRenderTarget(ren, NULL);
} }
@@ -147,43 +182,39 @@ void make_rapid()
{ {
SDL_SetRenderTarget(ren, tex); SDL_SetRenderTarget(ren, tex);
set_color(COLOR_DARKTEAL); set_color(COLOR_DARKTEAL);
for (int y=0;y<200;++y) for (int y=0;y<CAMP_HEIGHT;++y)
for (int x=0;x<200;++x) for (int x=0;x<CAMP_WIDTH;++x)
if (pixels[x+y*200]==COLOR_DARKRED && any_black(x,y)) if (pixels[x+y*CAMP_WIDTH]==COLOR_DARKRED && any_black(x,y))
{ {
pixels[x+y*200]=COLOR_DARKTEAL; pixels[x+y*CAMP_WIDTH]=COLOR_DARKTEAL;
SDL_RenderDrawPoint(ren, x, y); SDL_RenderDrawPoint(ren, x, y);
} }
SDL_SetRenderTarget(ren, NULL); SDL_SetRenderTarget(ren, NULL);
} }
void print(char *text, int x, int y, int grid, int offset)
{
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
srand(SDL_GetTicks()); srand(SDL_GetTicks());
SDL_Init(SDL_INIT_EVERYTHING); SDL_Init(SDL_INIT_EVERYTHING);
win = SDL_CreateWindow("JIX", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 960, 720, SDL_WINDOW_SHOWN); win = SDL_CreateWindow("JIX", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH*SCREEN_ZOOM, SCREEN_HEIGHT*SCREEN_ZOOM, SDL_WINDOW_SHOWN);
ren = SDL_CreateRenderer(win, -1, 0); ren = SDL_CreateRenderer(win, -1, 0);
SDL_RenderSetLogicalSize(ren, 320, 240); SDL_RenderSetLogicalSize(ren, SCREEN_WIDTH, SCREEN_HEIGHT);
tex = SDL_CreateTexture(ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, 200, 200); tex = SDL_CreateTexture(ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, CAMP_WIDTH, CAMP_HEIGHT);
gfx = SDL_CreateTextureFromSurface(ren, SDL_LoadBMP("jix.bmp")); gfx = SDL_CreateTextureFromSurface(ren, SDL_LoadBMP("jix.bmp"));
SDL_SetTextureBlendMode(gfx, SDL_BLENDMODE_ADD); SDL_SetTextureBlendMode(gfx, SDL_BLENDMODE_ADD);
for (int i=0; i<200*200; ++i) pixels[i]=0; for (int i=0; i<CAMP_WIDTH*CAMP_HEIGHT; ++i) pixels[i]=0;
for (int i=0; i<200; ++i) pixels[i]=pixels[i+198*200]=pixels[i*200]=pixels[198+i*200]=1; for (int i=0; i<CAMP_WIDTH; ++i) pixels[i]=pixels[i+(CAMP_HEIGHT-1)*CAMP_WIDTH]=1;
for (int i=0; i<CAMP_HEIGHT; ++i) pixels[i*CAMP_WIDTH]=pixels[CAMP_WIDTH-1+i*CAMP_WIDTH]=1;
SDL_SetRenderTarget(ren, tex); SDL_SetRenderTarget(ren, tex);
set_color(COLOR_BLACK); set_color(COLOR_BLACK);
SDL_RenderClear(ren); SDL_RenderClear(ren);
set_color(COLOR_WHITE); set_color(COLOR_WHITE);
SDL_RenderDrawLine(ren, 0, 0, 198, 0); SDL_RenderDrawLine(ren, 0, 0, CAMP_WIDTH-1, 0);
SDL_RenderDrawLine(ren, 0, 0, 0, 198); SDL_RenderDrawLine(ren, 0, 0, 0, CAMP_HEIGHT-1);
SDL_RenderDrawLine(ren, 198, 198, 198, 0); SDL_RenderDrawLine(ren, CAMP_WIDTH-1, CAMP_HEIGHT-1, CAMP_WIDTH-1, 0);
SDL_RenderDrawLine(ren, 198, 198, 0, 198); SDL_RenderDrawLine(ren, CAMP_WIDTH-1, CAMP_HEIGHT-1, 0, CAMP_HEIGHT-1);
SDL_SetRenderTarget(ren, NULL); SDL_SetRenderTarget(ren, NULL);
int should_exit = 0; int should_exit = 0;
@@ -207,36 +238,44 @@ int main(int argc, char *argv[])
if (lento) speed = 60; if (lento) speed = 60;
if (SDL_GetTicks()-t>=speed) if (SDL_GetTicks()-t>=speed)
{ {
if (keys[SDL_SCANCODE_UP] && player_y>0 && ( ((mode==MODE_CREANT || keys[SDL_SCANCODE_SPACE])&&pixels[player_x+(player_y-2)*200]==0) || pixels[player_x+(player_y-2)*200]==1)) { if (keys[SDL_SCANCODE_UP] && player_y>0 && ( ((mode==MODE_CREANT || keys[SDL_SCANCODE_SPACE])&&pixels[player_x+(player_y-2)*CAMP_WIDTH]==0) || pixels[player_x+(player_y-2)*CAMP_WIDTH]==1)) {
if (mode==MODE_CREANT) if (mode==MODE_CREANT)
put_pix(); put_pix();
else if (pixels[player_x+(player_y-2)*200]==0) else if (pixels[player_x+(player_y-2)*CAMP_WIDTH]==0) {
lento=1; lento=1;
mode = MODE_CREANT; player_y--; put_pix(); player_y--; mode = MODE_CREANT;
}
player_y--; put_pix(); player_y--;
} else if (keys[SDL_SCANCODE_RIGHT] && player_x<198 && ( ((mode==MODE_CREANT || keys[SDL_SCANCODE_SPACE])&&pixels[player_x+2+player_y*200]==0) || pixels[player_x+2+player_y*200]==1)) { } else if (keys[SDL_SCANCODE_RIGHT] && player_x<CAMP_WIDTH-1 && ( ((mode==MODE_CREANT || keys[SDL_SCANCODE_SPACE])&&pixels[player_x+2+player_y*CAMP_WIDTH]==0) || pixels[player_x+2+player_y*CAMP_WIDTH]==1)) {
if (mode==MODE_CREANT) if (mode==MODE_CREANT)
put_pix(); put_pix();
else if (pixels[player_x+2+player_y*200]==0) else if (pixels[player_x+2+player_y*CAMP_WIDTH]==0) {
lento=1; lento=1;
mode = MODE_CREANT; player_x++; put_pix(); player_x++; mode = MODE_CREANT;
}
player_x++; put_pix(); player_x++;
} else if (keys[SDL_SCANCODE_DOWN] && player_y<198 && ( ((mode==MODE_CREANT || keys[SDL_SCANCODE_SPACE])&&pixels[player_x+(player_y+2)*200]==0) || pixels[player_x+(player_y+2)*200]==1)) { } else if (keys[SDL_SCANCODE_DOWN] && player_y<CAMP_HEIGHT-1 && ( ((mode==MODE_CREANT || keys[SDL_SCANCODE_SPACE])&&pixels[player_x+(player_y+2)*CAMP_WIDTH]==0) || pixels[player_x+(player_y+2)*CAMP_WIDTH]==1)) {
if (mode==MODE_CREANT) if (mode==MODE_CREANT)
put_pix(); put_pix();
else if (pixels[player_x+(player_y+2)*200]==0) else if (pixels[player_x+(player_y+2)*CAMP_WIDTH]==0) {
lento=1; lento=1;
mode = MODE_CREANT; player_y++; put_pix(); player_y++; mode = MODE_CREANT;
}
player_y++; put_pix(); player_y++;
} else if (keys[SDL_SCANCODE_LEFT] && player_x>0 && ( ((mode==MODE_CREANT || keys[SDL_SCANCODE_SPACE])&&pixels[player_x-2+player_y*200]==0) || pixels[player_x-2+player_y*200]==1)) { } else if (keys[SDL_SCANCODE_LEFT] && player_x>0 && ( ((mode==MODE_CREANT || keys[SDL_SCANCODE_SPACE])&&pixels[player_x-2+player_y*CAMP_WIDTH]==0) || pixels[player_x-2+player_y*CAMP_WIDTH]==1)) {
if (mode==MODE_CREANT) if (mode==MODE_CREANT)
put_pix(); put_pix();
else if (pixels[player_x-2+player_y*200]==0) else if (pixels[player_x-2+player_y*CAMP_WIDTH]==0) {
lento=1; lento=1;
mode = MODE_CREANT; player_x--; put_pix(); player_x--; mode = MODE_CREANT;
}
player_x--; put_pix(); player_x--;
} }
@@ -248,7 +287,7 @@ int main(int argc, char *argv[])
make_rapid(); make_rapid();
lento=0; lento=0;
} }
if (pixels[player_x+player_y*200]==1) if (pixels[player_x+player_y*CAMP_WIDTH]==1)
{ {
close_sector(); close_sector();
mode = MODE_NORMAL; mode = MODE_NORMAL;
@@ -259,7 +298,11 @@ int main(int argc, char *argv[])
} }
set_color(COLOR_BLACK); set_color(COLOR_BLACK);
SDL_RenderClear(ren); SDL_RenderClear(ren);
SDL_Rect rect = {OFFSET_X,OFFSET_Y,200,200}; print("ABCDEFG", 16, 16, COLOR_WHITE, 8, -65);
print("HICJKLM", 16, 24, COLOR_WHITE, 8, -65);
print("NOOOOP", 24, 32, COLOR_WHITE, 8, -65);
SDL_Rect rect = {OFFSET_X,OFFSET_Y,CAMP_WIDTH,CAMP_HEIGHT};
SDL_RenderCopy(ren, tex, NULL, &rect); SDL_RenderCopy(ren, tex, NULL, &rect);
draw_player(); draw_player();
SDL_RenderPresent(ren); SDL_RenderPresent(ren);