From bb34a2bc029f6025fe29634a7099f464fce948f6 Mon Sep 17 00:00:00 2001 From: attilavs2 Date: Wed, 2 Jul 2025 14:31:35 +0200 Subject: [PATCH] =?UTF-8?q?-=20fuite=20m=C3=A9moire=20raylib,=20refactor?= =?UTF-8?q?=20de=20map?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script/test.lua | 4 ++-- src/config.h | 3 +++ src/draw.c | 32 ++++++++++++++++++++++++-------- src/draw.h | 2 -- src/game.c | 1 + src/lua_inter.c | 13 +++++++------ src/map.c | 3 ++- src/mapgen.c | 4 ++++ src/types.h | 2 +- src/ui.c | 5 ++--- 10 files changed, 46 insertions(+), 23 deletions(-) diff --git a/script/test.lua b/script/test.lua index 80b476d..c627b44 100644 --- a/script/test.lua +++ b/script/test.lua @@ -45,8 +45,8 @@ function on_tick(game) -- Ceci va drainer la sortie de la machine 2 -- (Le premier fourneau) local mach = get_machine_from_id(2) - if mach.storage[4] > 0 then - mach.storage[4] = mach.storage[4]-1 + if mach.storage[5] > 0 then + mach.storage[5] = mach.storage[5]-1 end end diff --git a/src/config.h b/src/config.h index 846819f..aa3b066 100644 --- a/src/config.h +++ b/src/config.h @@ -10,3 +10,6 @@ // Set in ui.c extern char *VERSION_STR; + +#define MAX_ZOOM 15.0f +#define MIN_ZOOM 0.27f diff --git a/src/draw.c b/src/draw.c index d77e370..7b1b4aa 100644 --- a/src/draw.c +++ b/src/draw.c @@ -54,7 +54,7 @@ char *tile_files[TILE_N] = { }; Texture2D tex_index[TEX_N]; -Image tile_index[TILE_N]; +Texture2D tile_index[TILE_N]; extern GUIInfo gui_info; @@ -75,13 +75,14 @@ int init_draw(){ } for(int i = 0; i < TILE_N; i++){ snprintf(tmpbuf, 4096, "%s%s", cwd, tile_files[i]); - tile_index[i] = LoadImage(tmpbuf); - ImageFormat(&tile_index[i], 4); + tile_index[i] = LoadTexture(tmpbuf); + GenTextureMipmaps(&tex_index[i]); } return 0; } +/* Texture2D mkmap_tex(Map *map){ uint8_t *tiles = map->layers[0]; void *pixels = MemAlloc(3*map->width*32*map->height*32); @@ -111,6 +112,7 @@ Texture2D mkmap_tex(Map *map){ MemFree(pixels); return maptex; } +*/ void draw_workers(Game *game){ Workers *workers = &game->workers; @@ -150,18 +152,32 @@ void draw_machines(Game *game){ } } -void draw_map(Game *game){ - DrawTextureV(game->map.maptex, (V2d){0,0}, WHITE); +void draw_map(Game *game, Camera2D cam){ + int w = game->map.width; + int bx = (cam.target.x - cam.offset.x)/32; + int mx = bx + 1280.0/32.0/cam.zoom + 2; + bx = bx < 0 ? 0 : bx; + mx = mx >= w ? w : mx; + int by = (cam.target.y - cam.offset.y)/32; + int my = by + 720.0/32.0/cam.zoom + 2; + by = by < 0 ? 0 : by; + my = my >= game->map.height ? game->map.height : my; + uint8_t *layer = game->map.layers[0]; + for(; by < my; by++){ + for(int nbx = bx; nbx < mx; nbx++) + DrawTexture(tile_index[layer[by*w+nbx]], nbx*32, by*32, WHITE); + } } void draw_game(Game *game){ - ClearBackground(BLACK); + ClearBackground((Color){144, 189, 209}); BeginMode2D(game->camera); - draw_map(game); + draw_map(game, game->camera); draw_machines(game); draw_workers(game); EndMode2D(); + printf("%f\n", game->camera.zoom); } void draw_game_mini(Game *game, Rectangle screen_box, Rectangle world_box){ @@ -178,7 +194,7 @@ void draw_game_mini(Game *game, Rectangle screen_box, Rectangle world_box){ BeginMode2D(ncam); BeginScissorMode(screen_box.x, screen_box.y, screen_box.width, screen_box.height); - draw_map(game); + draw_map(game, ncam); draw_machines(game); draw_workers(game); EndScissorMode(); diff --git a/src/draw.h b/src/draw.h index a69ecd7..189d1d3 100644 --- a/src/draw.h +++ b/src/draw.h @@ -18,8 +18,6 @@ int init_draw(); -Texture2D mkmap_tex(Map *map); - void draw_game(Game *game); void draw_game_mini(Game *game, Rectangle screen_box, Rectangle world_box); diff --git a/src/game.c b/src/game.c index 8869685..fd24aa4 100755 --- a/src/game.c +++ b/src/game.c @@ -364,6 +364,7 @@ int assign_worker_machine(Machine *machine, Worker *worker){ ai->assigned_machine = machine->id; ai->assigned_machine1 = MACH_NULL; ai->status = S_Operating; + // TODO : Don't tp worker->pos = machine->pos; return 0; } diff --git a/src/lua_inter.c b/src/lua_inter.c index 988d4e1..040fd2f 100644 --- a/src/lua_inter.c +++ b/src/lua_inter.c @@ -133,8 +133,8 @@ static int lua_storage_index(lua_State *L){ uint8_t **ptr = luaL_checkudata(L, 1, "Storage"); uint8_t *storage = *ptr; int pos = luaL_checkinteger(L, 2); - if(pos >= 0 && pos < 5){ - lua_pushinteger(L, storage[pos]); + if(pos > 0 && pos < 6){ + lua_pushinteger(L, storage[pos-1]); return 1; } else @@ -146,8 +146,8 @@ static int lua_storage_newindex(lua_State *L){ uint8_t *storage = *ptr; int pos = luaL_checkinteger(L, 2); int val = luaL_checkinteger(L, 3); - if(pos >= 0 && pos < 5) - storage[pos] = val; + if(pos > 0 && pos < 6) + storage[pos-1] = val; else return luaL_error(L, "Invalid index for Storage"); @@ -383,7 +383,7 @@ static int lua_get_machine_from_id(lua_State *L){ return 1; } -// add_worker(x, y) -> Machine +// add_worker(x, y) -> Worker static int lua_add_worker(lua_State *L){ lua_Number x = luaL_checknumber(L, 1); lua_Number y = luaL_checknumber(L, 2); @@ -586,8 +586,9 @@ void script_init(Game *game){ int ret = luaL_dofile(lstates[i], scripts.paths[i]); if(ret) puts(lua_tostring(lstates[i], -1)); - MemFree(scripts.paths[i]); } + for(int i = 0; i < scripts.capacity; i++) + MemFree(scripts.paths[i]); MemFree(scripts.paths); } diff --git a/src/map.c b/src/map.c index 85149b1..e9b84ae 100644 --- a/src/map.c +++ b/src/map.c @@ -39,10 +39,11 @@ int generate_coll(Game *game, int mapn){ int load_map(Game *game, int mapn){ LoadedMap *nmap = &game->map; Map *map = mapcollec.maps[mapn]; - nmap->maptex = mkmap_tex(map); nmap->width = map->width; nmap->height = map->height; nmap->coll = malloc(map->width*map->height); + nmap->layers[0] = map->layers[0]; + nmap->layers[1] = map->layers[1]; if(!nmap->coll) return 1; if(generate_coll(game, mapn)) diff --git a/src/mapgen.c b/src/mapgen.c index f501547..6e0187a 100644 --- a/src/mapgen.c +++ b/src/mapgen.c @@ -10,3 +10,7 @@ #include "config.h" #include "types.h" #include "mapgen.h" + +int gen_map(Game *game){ + +} diff --git a/src/types.h b/src/types.h index 7014d29..5d60c2c 100644 --- a/src/types.h +++ b/src/types.h @@ -123,10 +123,10 @@ typedef struct { typedef struct { + uint8_t *layers[2]; uint8_t *coll; int32_t width; // * int32_t height; // * - Texture2D maptex; } LoadedMap; diff --git a/src/ui.c b/src/ui.c index 8753e4b..fe7e93c 100644 --- a/src/ui.c +++ b/src/ui.c @@ -395,7 +395,6 @@ void get_keys(Game *game){ int key = GetKeyPressed(); while(key){ - printf("392 : %d\n", key); ui_do_key(game, key); key = GetKeyPressed(); } @@ -408,8 +407,8 @@ void get_keys(Game *game){ game->camera.target.x -= 3; if(keydown(KEY_D)) game->camera.target.x += 3; - if(keydown(KEY_UP)) + if(keydown(KEY_UP) && game->camera.zoom < MAX_ZOOM) game->camera.zoom *= 1.025; - if(keydown(KEY_DOWN)) + if(keydown(KEY_DOWN) && game->camera.zoom > MIN_ZOOM) game->camera.zoom *= 1/1.025; }