From 3fd5e282cf0c93217d08203e47e38025042bd081 Mon Sep 17 00:00:00 2001 From: Vladyslav Hrytsenko Date: Wed, 28 Sep 2022 21:40:59 +0300 Subject: [PATCH] code: minor qol changes --- code/foundation/src/debug/debug_ui.c | 120 ++++++++++--------- code/foundation/src/debug/debug_ui_widgets.c | 71 ++++++----- code/foundation/src/gen/assets.h | 1 + code/foundation/src/gen/assets_list.c | 5 +- code/foundation/src/gui/build_mode.c | 36 +++--- code/games/minimal/src/platform.c | 18 +++ code/games/minimal/src/texgen.c | 10 +- code/games/minimal_3d/src/platform.c | 18 +++ code/games/sandbox/src/texgen.c | 26 +++- 9 files changed, 190 insertions(+), 115 deletions(-) diff --git a/code/foundation/src/debug/debug_ui.c b/code/foundation/src/debug/debug_ui.c index 33c9ca1..c66056c 100644 --- a/code/foundation/src/debug/debug_ui.c +++ b/code/foundation/src/debug/debug_ui.c @@ -18,7 +18,7 @@ typedef enum { DITEM_LIST, DITEM_COND, DITEM_END, - + DITEM_FORCE_UINT8 = UINT8_MAX } debug_kind; @@ -54,28 +54,28 @@ typedef struct debug_item { float name_width; uint8_t skip; limit_kind limit_to; - + union { union { char const *text; uint64_t val; }; - + struct { struct debug_item *items; uint8_t is_collapsed; } list; - + struct { float val, min, max; void (*on_change)(float); } slider; - + void (*on_click)(void); - + uint8_t (*on_success)(void); }; - + debug_draw_result (*proc)(struct debug_item*, float, float); } debug_item; @@ -89,13 +89,15 @@ static int UIMeasureText(const char *text, int fontSize); static debug_item items[] = { { - .kind = DITEM_LIST, - .name = "general", + .kind = DITEM_LIST, + .name = "general", .list = { .is_collapsed = true, .items = (debug_item[]) { { .kind = DITEM_TEXT, .name = "delta time", .proc = DrawDeltaTime }, - { .kind = DITEM_TEXT, .name = "pos", .proc = DrawCameraPos }, + { .kind = DITEM_TEXT, .name = "camera pos", .proc = DrawCameraPos }, + { .kind = DITEM_TEXT, .name = "mouse block", .proc = DrawBlockPos }, + { .kind = DITEM_TEXT, .name = "mouse chunk", .proc = DrawChunkPos }, { .kind = DITEM_TEXT, .name = "zoom", .proc = DrawZoom }, { .kind = DITEM_END }, } @@ -109,17 +111,17 @@ static debug_item items[] = { .items = (debug_item[]) { { .kind = DITEM_COND, .on_success = CondIsWorldRunning }, { .kind = DITEM_BUTTON, .name = "pause", .on_click = ActWorldToggleSim }, - + { .kind = DITEM_COND, .on_success = CondIsWorldPaused, .skip = 6 }, { .kind = DITEM_BUTTON, .name = "resume", .on_click = ActWorldToggleSim }, - + { .kind = DITEM_GAP }, - + { .kind = DITEM_TEXT, .name = "step size", .proc = DrawWorldStepSize }, { .kind = DITEM_BUTTON, .name = "single-step", .on_click = ActWorldStep }, { .kind = DITEM_BUTTON, .name = "increment step size", .on_click = ActWorldIncrementSimStepSize }, { .kind = DITEM_BUTTON, .name = "decrement step size", .on_click = ActWorldDecrementSimStepSize }, - + { .kind = DITEM_END }, }, }, @@ -139,7 +141,7 @@ static debug_item items[] = { { .kind = DITEM_BUTTON, .name = "spawn chest", .on_click = ActSpawnChest }, { .kind = DITEM_BUTTON, .name = "spawn belt", .on_click = ActSpawnBelt }, { .kind = DITEM_BUTTON, .name = "spawn furnace", .on_click = ActSpawnFurnace }, - { + { .kind = DITEM_LIST, .name = "demo npcs", .list = { @@ -165,13 +167,13 @@ static debug_item items[] = { .items = (debug_item[]) { { .kind = DITEM_COND, .on_success = CondClientDisconnected }, { .kind = DITEM_TEXT, .name = "status", .proc = DrawLiteral, .text = "disconnected" }, - + { .kind = DITEM_COND, .on_success = CondClientConnected }, { .kind = DITEM_TEXT, .name = "status", .proc = DrawLiteral, .text = "connected" }, - + { .kind = DITEM_COND, .on_success = CondClientConnected }, { .kind = DITEM_TEXT, .proc = DrawNetworkStats }, - + { .kind = DITEM_END }, }, }, @@ -185,30 +187,30 @@ static debug_item items[] = { .items = (debug_item[]) { { .kind = DITEM_TEXT, .name = "macro", .proc = DrawReplayFileName }, { .kind = DITEM_TEXT, .name = "samples", .proc = DrawReplaySamples }, - + { .kind = DITEM_COND, .on_success = CondReplayDataPresentAndNotPlaying }, { .kind = DITEM_BUTTON, .name = "replay", .on_click = ActReplayRun }, - + { .kind = DITEM_COND, .on_success = CondReplayStatusOff }, { .kind = DITEM_BUTTON, .name = "record", .on_click = ActReplayBegin }, - + { .kind = DITEM_COND, .on_success = CondReplayStatusOn }, { .kind = DITEM_BUTTON, .name = "stop", .on_click = ActReplayEnd }, - + { .kind = DITEM_COND, .on_success = CondReplayIsPlaying }, { .kind = DITEM_BUTTON, .name = "stop", .on_click = ActReplayEnd }, - + { .kind = DITEM_COND, .on_success = CondReplayIsNotPlayingOrRecordsNotClear }, { .kind = DITEM_BUTTON, .name = "clear", .on_click = ActReplayClear }, - + { .kind = DITEM_GAP }, - + { .kind = DITEM_COND, .on_success = CondReplayIsNotPlaying, .skip = 4 }, { .kind = DITEM_BUTTON, .name = "new", .on_click = ActReplayNew }, { .kind = DITEM_BUTTON, .name = "load", .on_click = ActReplayLoad }, { .kind = DITEM_BUTTON, .name = "save", .on_click = ActReplaySave }, { .kind = DITEM_BUTTON, .name = "save as...", .on_click = ActReplaySaveAs }, - + { .kind = DITEM_END }, }, .is_collapsed = true, @@ -254,7 +256,7 @@ debug_draw_result debug_draw_list(debug_item *list, float xpos, float ypos, bool }break; case DITEM_COND: { ZPL_ASSERT(it->on_success); - + if (!it->on_success()) { it += it->skip ? it->skip : 1; } @@ -268,15 +270,15 @@ debug_draw_result debug_draw_list(debug_item *list, float xpos, float ypos, bool if (is_btn_pressed(xpos, ypos, it->name_width, DBG_FONT_SIZE, &color)) { it->list.is_collapsed = !it->list.is_collapsed; } - + UIDrawText(it->name, xpos, ypos, DBG_FONT_SIZE, color); ypos += DBG_FONT_SPACING; if (it->list.is_collapsed) break; - + debug_draw_result res = debug_draw_list(it->list.items, xpos+DBG_LIST_XPOS_OFFSET, ypos, is_shadow); ypos = res.y; }break; - + case DITEM_TEXT: { if (it->name) { char const *text = TextFormat("%s: ", it->name); @@ -286,18 +288,18 @@ debug_draw_result debug_draw_list(debug_item *list, float xpos, float ypos, bool UIDrawText(text, xpos, ypos, DBG_FONT_SIZE, RAYWHITE); ZPL_ASSERT(it->proc); } - + debug_draw_result res = it->proc(it, xpos + it->name_width, ypos); ypos = res.y; }break; - + case DITEM_RAW: { ZPL_ASSERT(it->proc); - + debug_draw_result res = it->proc(it, xpos, ypos); ypos = res.y; }break; - + case DITEM_BUTTON: { char const *text = TextFormat("> %s", it->name); if (it->name_width == 0) { @@ -307,15 +309,15 @@ debug_draw_result debug_draw_list(debug_item *list, float xpos, float ypos, bool if (is_btn_pressed(xpos, ypos, it->name_width, DBG_FONT_SIZE, &color) && it->on_click) { it->on_click(); } - + if (!it->on_click) { color = GRAY; } - + debug_draw_result res = DrawColoredText(xpos, ypos, text, color); ypos = res.y; }break; - + case DITEM_SLIDER: { ZPL_ASSERT(it->slider.min != it->slider.max); char const *text = TextFormat("%s: ", it->name); @@ -324,33 +326,33 @@ debug_draw_result debug_draw_list(debug_item *list, float xpos, float ypos, bool } UIDrawText(text, xpos, ypos, DBG_FONT_SIZE, RAYWHITE); xpos += it->name_width; - + DrawRectangleLines((int)xpos, (int)ypos, 100, DBG_FONT_SIZE, RAYWHITE); - + float stick_x = xpos + ((it->slider.val / it->slider.max) * 100.0f) - 5.0f; DrawRectangle((int)stick_x, (int)ypos, 10, DBG_FONT_SIZE, RED); - + xpos += 100.0f + 5.0f; DrawFloat(xpos, ypos, it->slider.val); ypos += DBG_FONT_SPACING; }break; - + default: { - + }break; } } - + return (debug_draw_result){xpos, ypos}; } void debug_draw(void) { // NOTE(zaklaus): Flush old debug samples debug_draw_flush(); - + float xpos = debug_xpos; float ypos = debug_ypos; - + // NOTE(zaklaus): move debug ui { debug_area_status area = check_mouse_area(xpos, ypos, DBG_CTRL_HANDLE_DIM, DBG_CTRL_HANDLE_DIM); @@ -360,19 +362,19 @@ void debug_draw(void) { color = RED; is_handle_ctrl_held = 1; } - + if (is_handle_ctrl_held) { debug_xpos = xpos = (float)(GetMouseX() - DBG_CTRL_HANDLE_DIM/2); debug_ypos = ypos = (float)(GetMouseY() - DBG_CTRL_HANDLE_DIM/2); - + if (area == DAREA_PRESS) { is_handle_ctrl_held = 0; } } - + DrawRectangle((int)xpos, (int)ypos, DBG_CTRL_HANDLE_DIM, DBG_CTRL_HANDLE_DIM, color); } - + // NOTE(zaklaus): toggle debug ui { Color color = BLUE; @@ -386,7 +388,7 @@ void debug_draw(void) { } DrawPoly((Vector2){xpos+DBG_CTRL_HANDLE_DIM/2, ypos+15+DBG_CTRL_HANDLE_DIM/2}, 3, 6.0f,is_debug_open ? 0.0f : 180.0f, color); } - + if (is_debug_open) { xpos += 15; debug_draw_list(items, xpos+DBG_SHADOW_OFFSET_XPOS, ypos+DBG_SHADOW_OFFSET_YPOS, 1); // NOTE(zaklaus): draw shadow @@ -397,7 +399,7 @@ void debug_draw(void) { debug_area_status check_mouse_area(float xpos, float ypos, float w, float h) { if (is_shadow_rendered) return DAREA_OUTSIDE; bool is_inside = CheckCollisionPointRec(GetMousePosition(), (Rectangle){xpos, ypos, w, h}); - + if (is_inside) { return IsMouseButtonReleased(MOUSE_LEFT_BUTTON) ? DAREA_PRESS : IsMouseButtonDown(MOUSE_LEFT_BUTTON) ? DAREA_HELD : DAREA_HOVER; } @@ -416,34 +418,34 @@ bool is_btn_pressed(float xpos, float ypos, float w, float h, Color *color) { } else if (area == DAREA_HELD) { *color = RED; } - + return false; } -static inline +static inline void UIDrawText(const char *text, float posX, float posY, int fontSize, Color color) { // Check if default font has been loaded if (GetFontDefault().texture.id != 0) { Vector2 position = { (float)posX , (float)posY }; - + int defaultFontSize = 10; // Default Font chars height in pixel int new_spacing = fontSize/defaultFontSize; - + DrawTextEx(GetFontDefault(), text, position, (float)fontSize , (float)new_spacing , is_shadow_rendered ? BLACK : color); } } -static inline +static inline int UIMeasureText(const char *text, int fontSize) { Vector2 vec = { 0.0f, 0.0f }; - + // Check if default font has been loaded if (GetFontDefault().texture.id != 0) { int defaultFontSize = 10; // Default Font chars height in pixel int new_spacing = fontSize/defaultFontSize; - + vec = MeasureTextEx(GetFontDefault(), text, (float)fontSize, (float)new_spacing); } - + return (int)vec.x; } diff --git a/code/foundation/src/debug/debug_ui_widgets.c b/code/foundation/src/debug/debug_ui_widgets.c index 16210d3..668b2f1 100644 --- a/code/foundation/src/debug/debug_ui_widgets.c +++ b/code/foundation/src/debug/debug_ui_widgets.c @@ -6,79 +6,96 @@ //~ NOTE(zaklaus): helpers -static inline debug_draw_result +static inline debug_draw_result DrawFloat(float xpos, float ypos, float val) { char const *text = TextFormat("%.02f\n", val); UIDrawText(text, xpos, ypos, DBG_FONT_SIZE, RAYWHITE); return (debug_draw_result){.x = xpos + UIMeasureText(text, DBG_FONT_SIZE), .y = ypos + DBG_FONT_SPACING}; } -static inline debug_draw_result +static inline debug_draw_result DrawColoredText(float xpos, float ypos, char const *text, Color color) { ZPL_ASSERT(text); UIDrawText(text, xpos, ypos, DBG_FONT_SIZE, color); - + char const *p = text; uint8_t newlines = 1; - + do { if (*p == '\n') ++newlines; } while (*p++ != 0); - + return (debug_draw_result){.x = xpos + UIMeasureText(text, DBG_FONT_SIZE), .y = ypos + DBG_FONT_SPACING*newlines}; } -static inline debug_draw_result +static inline debug_draw_result DrawFormattedText(float xpos, float ypos, char const *text) { return DrawColoredText(xpos, ypos, text, RAYWHITE); } //~ NOTE(zaklaus): widgets -static inline debug_draw_result +static inline debug_draw_result DrawCameraPos(debug_item *it, float xpos, float ypos) { (void)it; camera cam = camera_get(); return DrawFormattedText(xpos, ypos, TextFormat("%d %d", (int)(cam.x/WORLD_BLOCK_SIZE), (int)(cam.y/WORLD_BLOCK_SIZE))); } -static inline debug_draw_result +static inline debug_draw_result +DrawChunkPos(debug_item *it, float xpos, float ypos) { + float mx, my; + platform_get_block_realpos(&mx, &my); + int csize = world_chunk_size(); + + return DrawFormattedText(xpos, ypos, TextFormat("%d %d", (int)(mx/WORLD_BLOCK_SIZE/csize), (int)(my/WORLD_BLOCK_SIZE/csize))); +} + +static inline debug_draw_result +DrawBlockPos(debug_item *it, float xpos, float ypos) { + float mx, my; + platform_get_block_realpos(&mx, &my); + + return DrawFormattedText(xpos, ypos, TextFormat("%d %d", (int)(mx/WORLD_BLOCK_SIZE), (int)(my/WORLD_BLOCK_SIZE))); +} + +static inline debug_draw_result DrawUnmeasuredTime(debug_item *it, float xpos, float ypos) { (void)it; float total_time = (float)profiler_delta(PROF_TOTAL_TIME); float acc_time = (float)profiler_delta(PROF_MAIN_LOOP); - + return DrawFormattedText(xpos, ypos, TextFormat("%.02f ms", (total_time-acc_time) * 1000.0f)); } -static inline debug_draw_result +static inline debug_draw_result DrawDeltaTime(debug_item *it, float xpos, float ypos) { (void)it; float dt = GetFrameTime(); return DrawFormattedText(xpos, ypos, TextFormat("%.02f (%.02f fps)", dt * 1000.0f, 1.0f/dt)); } -static inline debug_draw_result +static inline debug_draw_result DrawZoom(debug_item *it, float xpos, float ypos) { (void)it; - + return DrawFloat(xpos, ypos, platform_zoom_get()); } -static inline debug_draw_result +static inline debug_draw_result DrawLiteral(debug_item *it, float xpos, float ypos) { ZPL_ASSERT(it->text); return DrawFormattedText(xpos, ypos, it->text); } -static inline debug_draw_result +static inline debug_draw_result DrawProfilerDelta(debug_item *it, float xpos, float ypos) { float dt = (float)profiler_delta(it->val); return DrawFormattedText(xpos, ypos, TextFormat("%s: %.02f ms", profiler_name(it->val), dt * 1000.0f)); } -static inline debug_draw_result +static inline debug_draw_result DrawReplaySamples(debug_item *it, float xpos, float ypos) { (void)it; size_t cnt = 0; @@ -88,7 +105,7 @@ DrawReplaySamples(debug_item *it, float xpos, float ypos) { return DrawFormattedText(xpos, ypos, TextFormat("%d of %d", record_pos, cnt)); } -static inline debug_draw_result +static inline debug_draw_result DrawReplayFileName(debug_item *it, float xpos, float ypos) { (void)it; return DrawFormattedText(xpos, ypos, TextFormat("%s", replay_filename[0] ? replay_filename : "")); @@ -96,41 +113,41 @@ DrawReplayFileName(debug_item *it, float xpos, float ypos) { // NOTE(zaklaus): demo npcs -static inline debug_draw_result +static inline debug_draw_result DrawDemoNPCCount(debug_item *it, float xpos, float ypos) { (void)it; return DrawFormattedText(xpos, ypos, TextFormat("%d", demo_npcs ? zpl_array_count(demo_npcs) : 0)); } -// NOTE(zaklaus): world simulation -static inline debug_draw_result +// NOTE(zaklaus): world simulation +static inline debug_draw_result DrawWorldStepSize(debug_item *it, float xpos, float ypos) { (void)it; return DrawFormattedText(xpos, ypos, TextFormat("%d ms", (int16_t)(sim_step_size*1000.f))); } // NOTE(zaklaus): network stats -static inline debug_draw_result +static inline debug_draw_result DrawNetworkStats(debug_item *it, float xpos, float ypos) { (void)it; - + network_client_stats s = network_client_fetch_stats(); - + #define _kb(x) ((x) / 1024) - + debug_draw_result r; r = DrawFormattedText(xpos, ypos, TextFormat("recv total: %lld kb", _kb(s.total_received))); r = DrawFormattedText(xpos, r.y, TextFormat("sent total: %lld kb", _kb(s.total_sent))); - + r = DrawFormattedText(xpos, r.y, TextFormat("dn rate: %.02f kb/sec (%.02f kbit/sec)", _kb(s.incoming_bandwidth), _kb(s.incoming_bandwidth * 8.0f))); r = DrawFormattedText(xpos, r.y, TextFormat("up rate: %.02f kb/sec (%.02f kbit/sec)", _kb(s.outgoing_bandwidth), _kb(s.outgoing_bandwidth * 8.0f))); - + r = DrawFormattedText(xpos, r.y, TextFormat("packets sent: %lld", s.packets_sent)); r = DrawFormattedText(xpos, r.y, TextFormat("packets lost: %d (%.02f%%)", s.packets_lost, s.packet_loss)); - + r = DrawFormattedText(xpos, r.y, TextFormat("ping: %d ms", s.ping)); - + #undef _kb return r; } diff --git a/code/foundation/src/gen/assets.h b/code/foundation/src/gen/assets.h index c245b8d..0550309 100644 --- a/code/foundation/src/gen/assets.h +++ b/code/foundation/src/gen/assets.h @@ -7,6 +7,7 @@ typedef enum { // NOTE(zaklaus): Debug ASSET_EMPTY, ASSET_BLANK, + ASSET_BLOCK_FRAME, ASSET_BUILDMODE_HIGHLIGHT, // NOTE(zaklaus): entities diff --git a/code/foundation/src/gen/assets_list.c b/code/foundation/src/gen/assets_list.c index e5b8340..e3cc374 100644 --- a/code/foundation/src/gen/assets_list.c +++ b/code/foundation/src/gen/assets_list.c @@ -13,11 +13,12 @@ static asset assets[] = { ASSET_TEX(ASSET_EMPTY), ASSET_TEX(ASSET_BLANK), + ASSET_TEX(ASSET_BLOCK_FRAME), ASSET_TEX(ASSET_BUILDMODE_HIGHLIGHT), ASSET_TEX(ASSET_DEMO_ICEMAKER), ASSET_TEX(ASSET_CHEST), ASSET_TEX(ASSET_FURNACE), - + // NOTE(zaklaus): blocks ASSET_TEX(ASSET_FENCE), ASSET_TEX(ASSET_DEV), @@ -31,7 +32,7 @@ static asset assets[] = { ASSET_TEX(ASSET_HOLE), ASSET_TEX(ASSET_WOOD), ASSET_TEX(ASSET_TREE), - + ASSET_TEX(ASSET_BELT), ASSET_TEX(ASSET_BELT_LEFT), ASSET_TEX(ASSET_BELT_RIGHT), diff --git a/code/foundation/src/gui/build_mode.c b/code/foundation/src/gui/build_mode.c index 772b117..138c4d1 100644 --- a/code/foundation/src/gui/build_mode.c +++ b/code/foundation/src/gui/build_mode.c @@ -27,24 +27,26 @@ void buildmode_draw(void) { platform_get_block_realpos(&mx, &my); cam.x = (double)mx; cam.y = (double)my; - + + renderer_draw_single(cam.x, cam.y, ASSET_BLOCK_FRAME, WHITE); + // NOTE(zaklaus): Check distance double dx = old_cam.x - cam.x; double dy = old_cam.y - cam.y; double dsq = (dx*dx + dy*dy); bool is_outside_range = (dsq > zpl_square(WORLD_BLOCK_SIZE*14)); - + if (build_submit_placements) { build_submit_placements = false; buildmode_clear_buffers(); } - + if (IsKeyPressed(KEY_B)){ build_is_deletion_mode = !build_is_deletion_mode; } - + Item *item = &e->items[e->selected_item]; - + if (e->has_items && !e->inside_vehicle && item->quantity > 0 && (!is_outside_range || build_is_deletion_mode)) { item_usage usage = 0; uint16_t item_id = 0; @@ -58,20 +60,20 @@ void buildmode_draw(void) { build_num_placements = 0; buildmode_clear_buffers(); } - + uint32_t qty = BUILD_MAX_PLACEMENTS; bool directional = false; - + if (!build_is_deletion_mode){ directional = item_get_place_directional(item_id); qty = item->quantity; } - + world_block_lookup l = world_block_from_realpos((float)cam.x, (float)cam.y); if (build_is_deletion_mode && !l.is_outer){ goto build_skip_placements; } - + if (build_is_in_draw_mode) { for (size_t i = 0; i < BUILD_MAX_PLACEMENTS; i++) { item_placement *it = &build_placements[i]; @@ -87,7 +89,7 @@ void buildmode_draw(void) { float sy = zpl_sign0(p2y-p1y); float sxx = zpl_sign0(p3x-p1x); float syy = zpl_sign0(p3y-p1y); - + if (sx != sxx || sy != syy) break; } it->x = (float)cam.x; @@ -100,31 +102,31 @@ void buildmode_draw(void) { } } } - - + + if (!is_outside_range) renderer_draw_single((float)cam.x, (float)cam.y, ASSET_BUILDMODE_HIGHLIGHT, ColorAlpha(build_is_deletion_mode ? RED : WHITE, 0.2f)); - + build_skip_placements: build_num_placements = zpl_min(build_num_placements, qty); } } - + for (size_t i = 0; i < build_num_placements; i++) { item_placement *it = &build_placements[i]; renderer_draw_single(it->x, it->y, ASSET_BUILDMODE_HIGHLIGHT, ColorAlpha(build_is_deletion_mode ? RED : WHITE, 0.4f)); } - + if (build_is_in_draw_mode) { if (IsKeyPressed(KEY_SPACE)) { build_is_in_draw_mode = false; buildmode_clear_buffers(); } - + if (IsMouseButtonReleased(MOUSE_RIGHT_BUTTON)) { build_submit_placements = true; build_is_in_draw_mode = false; } } - + } diff --git a/code/games/minimal/src/platform.c b/code/games/minimal/src/platform.c index f0ddbc4..9661446 100644 --- a/code/games/minimal/src/platform.c +++ b/code/games/minimal/src/platform.c @@ -169,3 +169,21 @@ float platform_zoom_get(void) { void platform_request_close(void) { request_shutdown = true; } + +void platform_get_block_realpos(float *x, float *y){ + camera cam = camera_get(); + Vector2 mpos = GetMousePosition(); + entity_view *e = game_world_view_active_get_entity(cam.ent_id); + if (!e) return; + float zoom = renderer_zoom_get(); + mpos.x -= screenWidth/2.0f; + mpos.y -= screenHeight/2.0f; + cam.x += mpos.x*(1.0f/zoom); + cam.y += mpos.y*(1.0f/zoom); + cam.x = ((int32_t)cam.x / (int32_t)(WORLD_BLOCK_SIZE)) * WORLD_BLOCK_SIZE; + cam.y = ((int32_t)cam.y / (int32_t)(WORLD_BLOCK_SIZE)) * WORLD_BLOCK_SIZE; + cam.x += WORLD_BLOCK_SIZE/2.0f; + cam.y += WORLD_BLOCK_SIZE/2.0f; + if (x) *x = (float)cam.x; + if (y) *y = (float)cam.y; +} diff --git a/code/games/minimal/src/texgen.c b/code/games/minimal/src/texgen.c index 674d681..ee6d358 100644 --- a/code/games/minimal/src/texgen.c +++ b/code/games/minimal/src/texgen.c @@ -37,7 +37,7 @@ Texture2D texgen_build_anim(asset_id id, int64_t counter) { ImageColorBrightness(&img, zpl_abs((counter % 64 - 32)*2)); return Image2TexEco(img); }break; - + default: return GenColorEco(PINK); break; } } @@ -46,10 +46,10 @@ Texture2D texgen_build_sprite(asset_id id) { switch (id) { case ASSET_BLANK: return GenColorEco(WHITE); break; case ASSET_BUILDMODE_HIGHLIGHT: return GenColorEco(WHITE); break; - + // NOTE(zaklaus): items case ASSET_DEMO_ICEMAKER: return LoadTexEco("demo_icemaker"); - + // NOTE(zaklaus): blocks case ASSET_FENCE: return LoadTexEco("fence"); case ASSET_GROUND: return LoadTexEco("grass"); @@ -67,10 +67,10 @@ Texture2D texgen_build_sprite(asset_id id) { case ASSET_BELT_LEFT: return LoadTexEco("belt_left"); case ASSET_BELT_UP: return LoadTexEco("belt_up"); case ASSET_BELT_DOWN: return LoadTexEco("belt_down"); - + // NOTE(zaklaus): devices case ASSET_CHEST: return LoadTexEco("chest"); - + default: return GenColorEco(PINK); break; } } diff --git a/code/games/minimal_3d/src/platform.c b/code/games/minimal_3d/src/platform.c index f0ddbc4..9661446 100644 --- a/code/games/minimal_3d/src/platform.c +++ b/code/games/minimal_3d/src/platform.c @@ -169,3 +169,21 @@ float platform_zoom_get(void) { void platform_request_close(void) { request_shutdown = true; } + +void platform_get_block_realpos(float *x, float *y){ + camera cam = camera_get(); + Vector2 mpos = GetMousePosition(); + entity_view *e = game_world_view_active_get_entity(cam.ent_id); + if (!e) return; + float zoom = renderer_zoom_get(); + mpos.x -= screenWidth/2.0f; + mpos.y -= screenHeight/2.0f; + cam.x += mpos.x*(1.0f/zoom); + cam.y += mpos.y*(1.0f/zoom); + cam.x = ((int32_t)cam.x / (int32_t)(WORLD_BLOCK_SIZE)) * WORLD_BLOCK_SIZE; + cam.y = ((int32_t)cam.y / (int32_t)(WORLD_BLOCK_SIZE)) * WORLD_BLOCK_SIZE; + cam.x += WORLD_BLOCK_SIZE/2.0f; + cam.y += WORLD_BLOCK_SIZE/2.0f; + if (x) *x = (float)cam.x; + if (y) *y = (float)cam.y; +} diff --git a/code/games/sandbox/src/texgen.c b/code/games/sandbox/src/texgen.c index 674d681..03bc09b 100644 --- a/code/games/sandbox/src/texgen.c +++ b/code/games/sandbox/src/texgen.c @@ -29,6 +29,21 @@ Texture2D GenColorEco(Color color) { return Image2TexEco(img); } +static inline +Texture2D GenFrameRect() { + RenderTexture2D temp_texture = LoadRenderTexture(64, 64); + + Color mouse_color_a = {0, 0, 0, 200}; + Color mouse_color_b = {255, 255, 255, 200}; + + BeginTextureMode(temp_texture); + DrawRectangleLines(0, 0, 64, 64, mouse_color_a); + DrawRectangleLines(1, 1, 62, 62, mouse_color_b); + EndTextureMode(); + + return temp_texture.texture; +} + Texture2D texgen_build_anim(asset_id id, int64_t counter) { (void)counter; switch (id) { @@ -37,7 +52,7 @@ Texture2D texgen_build_anim(asset_id id, int64_t counter) { ImageColorBrightness(&img, zpl_abs((counter % 64 - 32)*2)); return Image2TexEco(img); }break; - + default: return GenColorEco(PINK); break; } } @@ -46,10 +61,11 @@ Texture2D texgen_build_sprite(asset_id id) { switch (id) { case ASSET_BLANK: return GenColorEco(WHITE); break; case ASSET_BUILDMODE_HIGHLIGHT: return GenColorEco(WHITE); break; - + case ASSET_BLOCK_FRAME: return GenFrameRect(); break; + // NOTE(zaklaus): items case ASSET_DEMO_ICEMAKER: return LoadTexEco("demo_icemaker"); - + // NOTE(zaklaus): blocks case ASSET_FENCE: return LoadTexEco("fence"); case ASSET_GROUND: return LoadTexEco("grass"); @@ -67,10 +83,10 @@ Texture2D texgen_build_sprite(asset_id id) { case ASSET_BELT_LEFT: return LoadTexEco("belt_left"); case ASSET_BELT_UP: return LoadTexEco("belt_up"); case ASSET_BELT_DOWN: return LoadTexEco("belt_down"); - + // NOTE(zaklaus): devices case ASSET_CHEST: return LoadTexEco("chest"); - + default: return GenColorEco(PINK); break; } }