- fuite mémoire raylib, refactor de map

This commit is contained in:
attilavs2 2025-07-02 14:31:35 +02:00
parent 80688b4e26
commit bb34a2bc02
10 changed files with 46 additions and 23 deletions

View file

@ -45,8 +45,8 @@ function on_tick(game)
-- Ceci va drainer la sortie de la machine 2 -- Ceci va drainer la sortie de la machine 2
-- (Le premier fourneau) -- (Le premier fourneau)
local mach = get_machine_from_id(2) local mach = get_machine_from_id(2)
if mach.storage[4] > 0 then if mach.storage[5] > 0 then
mach.storage[4] = mach.storage[4]-1 mach.storage[5] = mach.storage[5]-1
end end
end end

View file

@ -10,3 +10,6 @@
// Set in ui.c // Set in ui.c
extern char *VERSION_STR; extern char *VERSION_STR;
#define MAX_ZOOM 15.0f
#define MIN_ZOOM 0.27f

View file

@ -54,7 +54,7 @@ char *tile_files[TILE_N] = {
}; };
Texture2D tex_index[TEX_N]; Texture2D tex_index[TEX_N];
Image tile_index[TILE_N]; Texture2D tile_index[TILE_N];
extern GUIInfo gui_info; extern GUIInfo gui_info;
@ -75,13 +75,14 @@ int init_draw(){
} }
for(int i = 0; i < TILE_N; i++){ for(int i = 0; i < TILE_N; i++){
snprintf(tmpbuf, 4096, "%s%s", cwd, tile_files[i]); snprintf(tmpbuf, 4096, "%s%s", cwd, tile_files[i]);
tile_index[i] = LoadImage(tmpbuf); tile_index[i] = LoadTexture(tmpbuf);
ImageFormat(&tile_index[i], 4); GenTextureMipmaps(&tex_index[i]);
} }
return 0; return 0;
} }
/*
Texture2D mkmap_tex(Map *map){ Texture2D mkmap_tex(Map *map){
uint8_t *tiles = map->layers[0]; uint8_t *tiles = map->layers[0];
void *pixels = MemAlloc(3*map->width*32*map->height*32); void *pixels = MemAlloc(3*map->width*32*map->height*32);
@ -111,6 +112,7 @@ Texture2D mkmap_tex(Map *map){
MemFree(pixels); MemFree(pixels);
return maptex; return maptex;
} }
*/
void draw_workers(Game *game){ void draw_workers(Game *game){
Workers *workers = &game->workers; Workers *workers = &game->workers;
@ -150,18 +152,32 @@ void draw_machines(Game *game){
} }
} }
void draw_map(Game *game){ void draw_map(Game *game, Camera2D cam){
DrawTextureV(game->map.maptex, (V2d){0,0}, WHITE); 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){ void draw_game(Game *game){
ClearBackground(BLACK); ClearBackground((Color){144, 189, 209});
BeginMode2D(game->camera); BeginMode2D(game->camera);
draw_map(game); draw_map(game, game->camera);
draw_machines(game); draw_machines(game);
draw_workers(game); draw_workers(game);
EndMode2D(); EndMode2D();
printf("%f\n", game->camera.zoom);
} }
void draw_game_mini(Game *game, Rectangle screen_box, Rectangle world_box){ 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); BeginMode2D(ncam);
BeginScissorMode(screen_box.x, screen_box.y, screen_box.width, BeginScissorMode(screen_box.x, screen_box.y, screen_box.width,
screen_box.height); screen_box.height);
draw_map(game); draw_map(game, ncam);
draw_machines(game); draw_machines(game);
draw_workers(game); draw_workers(game);
EndScissorMode(); EndScissorMode();

View file

@ -18,8 +18,6 @@
int init_draw(); int init_draw();
Texture2D mkmap_tex(Map *map);
void draw_game(Game *game); void draw_game(Game *game);
void draw_game_mini(Game *game, Rectangle screen_box, Rectangle world_box); void draw_game_mini(Game *game, Rectangle screen_box, Rectangle world_box);

View file

@ -364,6 +364,7 @@ int assign_worker_machine(Machine *machine, Worker *worker){
ai->assigned_machine = machine->id; ai->assigned_machine = machine->id;
ai->assigned_machine1 = MACH_NULL; ai->assigned_machine1 = MACH_NULL;
ai->status = S_Operating; ai->status = S_Operating;
// TODO : Don't tp
worker->pos = machine->pos; worker->pos = machine->pos;
return 0; return 0;
} }

View file

@ -133,8 +133,8 @@ static int lua_storage_index(lua_State *L){
uint8_t **ptr = luaL_checkudata(L, 1, "Storage"); uint8_t **ptr = luaL_checkudata(L, 1, "Storage");
uint8_t *storage = *ptr; uint8_t *storage = *ptr;
int pos = luaL_checkinteger(L, 2); int pos = luaL_checkinteger(L, 2);
if(pos >= 0 && pos < 5){ if(pos > 0 && pos < 6){
lua_pushinteger(L, storage[pos]); lua_pushinteger(L, storage[pos-1]);
return 1; return 1;
} }
else else
@ -146,8 +146,8 @@ static int lua_storage_newindex(lua_State *L){
uint8_t *storage = *ptr; uint8_t *storage = *ptr;
int pos = luaL_checkinteger(L, 2); int pos = luaL_checkinteger(L, 2);
int val = luaL_checkinteger(L, 3); int val = luaL_checkinteger(L, 3);
if(pos >= 0 && pos < 5) if(pos > 0 && pos < 6)
storage[pos] = val; storage[pos-1] = val;
else else
return luaL_error(L, "Invalid index for Storage"); return luaL_error(L, "Invalid index for Storage");
@ -383,7 +383,7 @@ static int lua_get_machine_from_id(lua_State *L){
return 1; return 1;
} }
// add_worker(x, y) -> Machine // add_worker(x, y) -> Worker
static int lua_add_worker(lua_State *L){ static int lua_add_worker(lua_State *L){
lua_Number x = luaL_checknumber(L, 1); lua_Number x = luaL_checknumber(L, 1);
lua_Number y = luaL_checknumber(L, 2); 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]); int ret = luaL_dofile(lstates[i], scripts.paths[i]);
if(ret) if(ret)
puts(lua_tostring(lstates[i], -1)); 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); MemFree(scripts.paths);
} }

View file

@ -39,10 +39,11 @@ int generate_coll(Game *game, int mapn){
int load_map(Game *game, int mapn){ int load_map(Game *game, int mapn){
LoadedMap *nmap = &game->map; LoadedMap *nmap = &game->map;
Map *map = mapcollec.maps[mapn]; Map *map = mapcollec.maps[mapn];
nmap->maptex = mkmap_tex(map);
nmap->width = map->width; nmap->width = map->width;
nmap->height = map->height; nmap->height = map->height;
nmap->coll = malloc(map->width*map->height); nmap->coll = malloc(map->width*map->height);
nmap->layers[0] = map->layers[0];
nmap->layers[1] = map->layers[1];
if(!nmap->coll) if(!nmap->coll)
return 1; return 1;
if(generate_coll(game, mapn)) if(generate_coll(game, mapn))

View file

@ -10,3 +10,7 @@
#include "config.h" #include "config.h"
#include "types.h" #include "types.h"
#include "mapgen.h" #include "mapgen.h"
int gen_map(Game *game){
}

View file

@ -123,10 +123,10 @@ typedef struct {
typedef struct { typedef struct {
uint8_t *layers[2];
uint8_t *coll; uint8_t *coll;
int32_t width; // * int32_t width; // *
int32_t height; // * int32_t height; // *
Texture2D maptex;
} LoadedMap; } LoadedMap;

View file

@ -395,7 +395,6 @@ void get_keys(Game *game){
int key = GetKeyPressed(); int key = GetKeyPressed();
while(key){ while(key){
printf("392 : %d\n", key);
ui_do_key(game, key); ui_do_key(game, key);
key = GetKeyPressed(); key = GetKeyPressed();
} }
@ -408,8 +407,8 @@ void get_keys(Game *game){
game->camera.target.x -= 3; game->camera.target.x -= 3;
if(keydown(KEY_D)) if(keydown(KEY_D))
game->camera.target.x += 3; game->camera.target.x += 3;
if(keydown(KEY_UP)) if(keydown(KEY_UP) && game->camera.zoom < MAX_ZOOM)
game->camera.zoom *= 1.025; game->camera.zoom *= 1.025;
if(keydown(KEY_DOWN)) if(keydown(KEY_DOWN) && game->camera.zoom > MIN_ZOOM)
game->camera.zoom *= 1/1.025; game->camera.zoom *= 1/1.025;
} }