+3
-2
include/keraforge/graphics.h
+3
-2
include/keraforge/graphics.h
···
6
7
8
/* Number of frames since the game opened.
9
-
You should only use this for animations!
10
-
It's not consistent enough for precise timing use deltatime (kf_dts/kf_dtms) for that. */
11
extern u64 kf_frame;
12
/* Deltatime in milliseconds */
13
extern f32 kf_dtms;
14
/* Deltatime in seconds. */
···
6
7
8
/* Number of frames since the game opened.
9
+
Not consistent enough for precise timing! Use deltatime (kf_dts/kf_dtms) instead. */
10
extern u64 kf_frame;
11
+
/* Number of seconds since the game opened. */
12
+
extern f64 kf_s;
13
/* Deltatime in milliseconds */
14
extern f32 kf_dtms;
15
/* Deltatime in seconds. */
+1
include/keraforge/world.h
+1
include/keraforge/world.h
+1
-1
src/actor.c
+1
-1
src/actor.c
+1
src/graphics.c
+1
src/graphics.c
+23
-33
src/main.c
+23
-33
src/main.c
···
1
#include <keraforge.h>
2
#include <raylib.h>
3
#include <raymath.h>
···
8
static Camera2D cam;
9
static struct kf_vec2(u32) select = { 0, 0 };
10
static int selected_tile = 0;
11
-
static bool selected_menu_this_frame = false;
12
static enum {
13
menu_none,
14
menu_palette,
15
-
menu_escape
16
} menu;
17
18
static kf_inputbind_t
19
inputbind_move_up,
···
30
inputbind_palette,
31
inputbind_zoom_reset,
32
inputbind_zoom_in,
33
-
inputbind_zoom_out
34
;
35
36
···
56
inputbind_zoom_reset = kf_addinput("zoom_reset", KEY_ZERO, KEY_NULL, MOUSE_BUTTON_UNKNOWN, GAMEPAD_BUTTON_UNKNOWN, GAMEPAD_AXIS_UNKNOWN);
57
inputbind_zoom_in = kf_addinput("zoom_in", KEY_EQUAL, KEY_NULL, MOUSE_BUTTON_UNKNOWN, GAMEPAD_BUTTON_UNKNOWN, GAMEPAD_AXIS_UNKNOWN);
58
inputbind_zoom_out = kf_addinput("zoom_out", KEY_MINUS, KEY_NULL, MOUSE_BUTTON_UNKNOWN, GAMEPAD_BUTTON_UNKNOWN, GAMEPAD_AXIS_UNKNOWN);
59
}
60
61
static
62
void setmenu(int m)
63
{
64
menu = m;
65
-
selected_menu_this_frame = true;
66
}
67
68
static
···
140
141
// SetTraceLogLevel(LOG_WARNING);
142
InitWindow(800, 600, "Keraforge");
143
-
SetTargetFPS(60);
144
SetExitKey(KEY_NULL);
145
146
loadbinds();
···
186
if (!kf_exists("data/map.bin"))
187
{
188
printf("-> creating world\n");
189
-
world = kf_world_new(128, 128, 1);
190
printf("-> saving world\n");
191
size_t len = kf_world_getsize(world);
192
printf("-> writing of %lu bytes\n", len);
···
241
if (kf_checkinputpress(inputbind_palette))
242
setmenu(menu_palette);
243
else if (kf_checkinputpress(inputbind_cancel) && menu == menu_none)
244
-
setmenu(menu_escape);
245
else if (kf_checkinputpress(inputbind_zoom_reset))
246
cam.zoom = 2;
247
else if (kf_checkinputpress(inputbind_zoom_in) && cam.zoom < 3.50f)
248
cam.zoom += 0.25f;
249
else if (kf_checkinputpress(inputbind_zoom_out) && cam.zoom > 1.00f)
250
cam.zoom -= 0.25f;
251
252
BeginDrawing();
253
ClearBackground(BLACK);
···
270
player->draw(world, player);
271
EndMode2D();
272
273
-
if (selected_menu_this_frame)
274
{
275
-
selected_menu_this_frame = false;
276
-
}
277
-
else
278
-
{
279
-
switch (menu)
280
-
{
281
-
case menu_none:
282
-
break;
283
-
case menu_palette:
284
-
if (kf_ui_choice("Select tile", &kf_tiles.key[0], kf_tiles.count + 1, &selected_tile))
285
-
setmenu(menu_none);
286
-
break;
287
-
case menu_escape:
288
-
{
289
-
static int result = 1;
290
-
if (kf_ui_yesno("Exit game?", &result))
291
-
{
292
-
menu = menu_none;
293
-
if (result)
294
-
running = 0;
295
-
result = 1;
296
-
}
297
-
break;
298
-
}
299
-
}
300
}
301
302
DrawFPS(0, 0);
303
DrawText(TextFormat("%f", kf_dts), 0, 20, 20, RED);
304
305
EndDrawing();
306
307
kf_frame++;
308
kf_dts = GetFrameTime();
309
kf_dtms = kf_dts * 1000;
310
}
311
312
if (world)
···
1
+
#include "keraforge/input.h"
2
#include <keraforge.h>
3
#include <raylib.h>
4
#include <raymath.h>
···
9
static Camera2D cam;
10
static struct kf_vec2(u32) select = { 0, 0 };
11
static int selected_tile = 0;
12
static enum {
13
menu_none,
14
menu_palette,
15
} menu;
16
+
static int target_fps = 60;
17
18
static kf_inputbind_t
19
inputbind_move_up,
···
30
inputbind_palette,
31
inputbind_zoom_reset,
32
inputbind_zoom_in,
33
+
inputbind_zoom_out,
34
+
inputbind_toggle_fps_limit
35
;
36
37
···
57
inputbind_zoom_reset = kf_addinput("zoom_reset", KEY_ZERO, KEY_NULL, MOUSE_BUTTON_UNKNOWN, GAMEPAD_BUTTON_UNKNOWN, GAMEPAD_AXIS_UNKNOWN);
58
inputbind_zoom_in = kf_addinput("zoom_in", KEY_EQUAL, KEY_NULL, MOUSE_BUTTON_UNKNOWN, GAMEPAD_BUTTON_UNKNOWN, GAMEPAD_AXIS_UNKNOWN);
59
inputbind_zoom_out = kf_addinput("zoom_out", KEY_MINUS, KEY_NULL, MOUSE_BUTTON_UNKNOWN, GAMEPAD_BUTTON_UNKNOWN, GAMEPAD_AXIS_UNKNOWN);
60
+
61
+
inputbind_toggle_fps_limit = kf_addinput("toggle_fps_limit", KEY_NINE, KEY_NULL, MOUSE_BUTTON_UNKNOWN, GAMEPAD_BUTTON_UNKNOWN, GAMEPAD_AXIS_UNKNOWN);
62
}
63
64
static
65
void setmenu(int m)
66
{
67
menu = m;
68
}
69
70
static
···
142
143
// SetTraceLogLevel(LOG_WARNING);
144
InitWindow(800, 600, "Keraforge");
145
+
SetTargetFPS(target_fps);
146
SetExitKey(KEY_NULL);
147
148
loadbinds();
···
188
if (!kf_exists("data/map.bin"))
189
{
190
printf("-> creating world\n");
191
+
world = kf_world_new(4096, 4096, 2);
192
printf("-> saving world\n");
193
size_t len = kf_world_getsize(world);
194
printf("-> writing of %lu bytes\n", len);
···
243
if (kf_checkinputpress(inputbind_palette))
244
setmenu(menu_palette);
245
else if (kf_checkinputpress(inputbind_cancel) && menu == menu_none)
246
+
running = 0;
247
else if (kf_checkinputpress(inputbind_zoom_reset))
248
cam.zoom = 2;
249
else if (kf_checkinputpress(inputbind_zoom_in) && cam.zoom < 3.50f)
250
cam.zoom += 0.25f;
251
else if (kf_checkinputpress(inputbind_zoom_out) && cam.zoom > 1.00f)
252
cam.zoom -= 0.25f;
253
+
else if (kf_checkinputpress(inputbind_toggle_fps_limit))
254
+
{
255
+
target_fps = target_fps <= 0 ? 60 : 0;
256
+
SetTargetFPS(target_fps);
257
+
}
258
259
BeginDrawing();
260
ClearBackground(BLACK);
···
277
player->draw(world, player);
278
EndMode2D();
279
280
+
switch (menu)
281
{
282
+
case menu_none:
283
+
break;
284
+
case menu_palette:
285
+
if (kf_ui_choice("Select tile", &kf_tiles.key[0], kf_tiles.count + 1, &selected_tile))
286
+
setmenu(menu_none);
287
+
break;
288
}
289
290
DrawFPS(0, 0);
291
DrawText(TextFormat("%f", kf_dts), 0, 20, 20, RED);
292
+
DrawText(TextFormat("%f", kf_s), 0, 40, 20, RED);
293
294
EndDrawing();
295
296
kf_frame++;
297
kf_dts = GetFrameTime();
298
kf_dtms = kf_dts * 1000;
299
+
kf_s += kf_dts;
300
}
301
302
if (world)
-1
src/math.c
-1
src/math.c
+16
-1
src/world.c
+16
-1
src/world.c
···
1
-
#include "keraforge/world.h"
2
#include <keraforge.h>
3
#include <raylib.h>
4
#include <stdio.h>
···
40
world->height = height;
41
memset(world->map, 0, sizeof(struct kf_tile)*width*height);
42
for (size_t i = 0 ; i < width*height ; i++)
43
world->map[i].id = fill;
44
45
u32 x;
46
for (u32 y = 0 ; y < height ; y++)
···
179
{
180
for (x = sx ; x < ex ; x++)
181
{
182
KF_SANITY_CHECK(tile->id <= kf_tiles.count, "erroneous tile on map at %u,%u: %u (count=%u)", x, y, tile->id, kf_tiles.count);
183
if (tile->id)
184
{
185
struct kf_vec2(u32) s = kf_getspritefortilebitmask(tile->data);
···
1
#include <keraforge.h>
2
#include <raylib.h>
3
#include <stdio.h>
···
39
world->height = height;
40
memset(world->map, 0, sizeof(struct kf_tile)*width*height);
41
for (size_t i = 0 ; i < width*height ; i++)
42
+
{
43
+
world->map[i].subid = fill;
44
world->map[i].id = fill;
45
+
}
46
47
u32 x;
48
for (u32 y = 0 ; y < height ; y++)
···
181
{
182
for (x = sx ; x < ex ; x++)
183
{
184
+
KF_SANITY_CHECK(tile->subid <= kf_tiles.count, "erroneous subtile on map at %u,%u: %u (count=%u)", x, y, tile->subid, kf_tiles.count);
185
KF_SANITY_CHECK(tile->id <= kf_tiles.count, "erroneous tile on map at %u,%u: %u (count=%u)", x, y, tile->id, kf_tiles.count);
186
+
if (tile->data != 0x1111 && tile->subid) /* 0x1111: full tile, no subtile rendering needed */
187
+
{
188
+
kf_drawsprite_wh(
189
+
kf_tiles.sheet[tile->subid],
190
+
x*KF_TILE_SIZE_PX,
191
+
y*KF_TILE_SIZE_PX,
192
+
KF_TILE_SIZE_PX,
193
+
KF_TILE_SIZE_PX,
194
+
kf_tiles.sprite[tile->subid].x + 2, /* 2,1 are offsets for the "middle" tile */
195
+
kf_tiles.sprite[tile->subid].y + 1
196
+
);
197
+
}
198
if (tile->id)
199
{
200
struct kf_vec2(u32) s = kf_getspritefortilebitmask(tile->data);