- fuite mémoire raylib, refactor de map
This commit is contained in:
parent
80688b4e26
commit
bb34a2bc02
10 changed files with 46 additions and 23 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -10,3 +10,6 @@
|
|||
|
||||
// Set in ui.c
|
||||
extern char *VERSION_STR;
|
||||
|
||||
#define MAX_ZOOM 15.0f
|
||||
#define MIN_ZOOM 0.27f
|
||||
|
|
32
src/draw.c
32
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();
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -10,3 +10,7 @@
|
|||
#include "config.h"
|
||||
#include "types.h"
|
||||
#include "mapgen.h"
|
||||
|
||||
int gen_map(Game *game){
|
||||
|
||||
}
|
||||
|
|
|
@ -123,10 +123,10 @@ typedef struct {
|
|||
|
||||
typedef struct {
|
||||
|
||||
uint8_t *layers[2];
|
||||
uint8_t *coll;
|
||||
int32_t width; // *
|
||||
int32_t height; // *
|
||||
Texture2D maptex;
|
||||
|
||||
} LoadedMap;
|
||||
|
||||
|
|
5
src/ui.c
5
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;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue