From 9d0bb9f229155546fde8b4f666d2682ad5ac606f Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Fri, 5 May 2023 20:09:22 +0200 Subject: lighting work --- src/editor.c | 124 +++++++++++++++++++++++++++++++++++++++++++---------------- src/map.c | 59 ++++++++++++++++++++-------- 2 files changed, 132 insertions(+), 51 deletions(-) (limited to 'src') diff --git a/src/editor.c b/src/editor.c index dd46e7d..b04c5dd 100644 --- a/src/editor.c +++ b/src/editor.c @@ -25,6 +25,41 @@ int editor_width = 200; int borderw = 8; int offset_y = 50; +static void update_tile_editor(platform_window* window) { + map_info info = get_map_info(window); + vec2 pos = screen_pos_to_world_pos(window, _global_mouse.x, _global_mouse.y); + + if (pos.x < 0 || pos.y < 0) return; + if (pos.x >= loaded_map.width || pos.y >= loaded_map.height) return; + + switch (tile_edit_state) + { + case PLACING_TILE: + if (is_left_down()) { + map_to_load.tiles[pos.y][pos.x] = tile_to_place; + load_mapdata_into_world(); + } + break; + + case RAISING_GROUND: + if (is_left_clicked()) { + map_to_load.heightmap[pos.y][pos.x]++; + load_mapdata_into_world(); + } + break; + + case LOWERING_GROUND: + if (is_left_clicked()) { + if (map_to_load.heightmap[pos.y][pos.x] > 0) map_to_load.heightmap[pos.y][pos.x]--; + load_mapdata_into_world(); + } + break; + + default: + break; + } +} + void update_editor(platform_window* window) { if (keyboard_is_key_pressed(KEY_F1)) { @@ -51,40 +86,14 @@ void update_editor(platform_window* window) _next_camera_pos.x = -(window->width / 2) + camera_x; _next_camera_pos.y = -(window->height / 2) + camera_y; - map_info info = get_map_info(window); - int mouse_tile_y = (_global_mouse.y + _global_camera.y) / info.tile_height; - int mouse_tile_x = (((_global_mouse.x + _global_camera.x) - (info.px_incline * mouse_tile_y)) / info.tile_width); - - if (mouse_tile_x < 0 || mouse_tile_y < 0) return; - if (mouse_tile_x >= loaded_map.width || mouse_tile_y >= loaded_map.height) return; - - switch (tile_edit_state) + switch (edit_state) { - case PLACING_TILE: - if (is_left_down()) { - map_to_load.tiles[mouse_tile_y][mouse_tile_x] = tile_to_place; - load_mapdata_into_world(); - } - break; - - case RAISING_GROUND: - if (is_left_clicked()) { - map_to_load.heightmap[mouse_tile_y][mouse_tile_x]++; - load_mapdata_into_world(); - } - break; - - case LOWERING_GROUND: - if (is_left_clicked()) { - if (map_to_load.heightmap[mouse_tile_y][mouse_tile_x] > 0) map_to_load.heightmap[mouse_tile_y][mouse_tile_x]--; - load_mapdata_into_world(); - } - break; - - default: - break; + case EDITING_TILES: update_tile_editor(window); break; + case EDITING_OBJECTS: break; + case EDITING_LIGHTING: break; } + if (keyboard_is_key_down(KEY_LEFT_CONTROL) && keyboard_is_key_pressed(KEY_S)) { platform_write_file_content("../data/maps/map1.dat", "wb", (u8*)&map_to_load, sizeof(map_to_load)); platform_write_file_content("data/maps/map1.dat", "wb", (u8*)&map_to_load, sizeof(map_to_load)); @@ -161,14 +170,61 @@ void draw_tile_panel(platform_window* window) { } void draw_lighting_panel(platform_window* window) { - int row_h = 40; + static bool dragging_light = false; + static int dragging_light_index = 0; + static int drag_start_x = 0; + static int drag_start_y = 0; + static float orig_x = 0; + static float orig_y = 0; + + int row_h = 20; + int offsety = 0; - int count = 0; for (int i = 0; i < MAX_LIGHT_EMITTERS; i++) { light_emitter emitter = loaded_map.light_emitters[i]; if (!emitter.active) continue; - renderer->render_rectangle(_global_camera.x, _global_camera.y + offset_y + ((row_h+1)*count), editor_width, row_h, rgba(255,0,0,40)); + renderer->render_rectangle(_global_camera.x, _global_camera.y + offset_y + row_h + offsety, editor_width, row_h, rgba(255,0,0,40)); + + char buf[50]; + sprintf(buf, "{x: %.0f y: %.0f, z: %.0f}", emitter.position.x, emitter.position.y, emitter.position.z); + renderer->render_text(fnt_20, _global_camera.x, _global_camera.y + offset_y + row_h + offsety + 5, buf, rgb(0,0,0)); + + vec2f pos = world_pos_to_screen_pos(window, emitter.position.x, emitter.position.y, emitter.position.z); + renderer->render_rectangle(pos.x-3, pos.y-3, 36, 36, rgb(100,0,0)); + renderer->render_rectangle(pos.x, pos.y, 30, 30, rgb(255,0,0)); + renderer->render_image(img_sunny, pos.x, pos.y, 30, 30); + + if (_global_mouse.x + _global_camera.x > pos.x && + _global_mouse.x + _global_camera.x < pos.x + 30 && + _global_mouse.y + _global_camera.y > pos.y && + _global_mouse.y + _global_camera.y < pos.y + 30) { + if (is_left_clicked()) { + dragging_light = true; + dragging_light_index = i; + drag_start_x = _global_mouse.x; + drag_start_y = _global_mouse.y; + orig_x = emitter.position.x; + orig_y = emitter.position.y; + } + } + + if (dragging_light && dragging_light_index == i) { + if (is_left_down()) { + int newx = (_global_mouse.x - drag_start_x) - _global_camera.x; + int newy = (_global_mouse.y - drag_start_y) - _global_camera.y; + vec2 newpos = screen_pos_to_world_pos(window, newx, newy); + + map_to_load.light_emitters[i].position.x = orig_x + newpos.x; + map_to_load.light_emitters[i].position.y = orig_y + newpos.y; + load_mapdata_into_world(); + } + else { + dragging_light = false; + } + } + + offsety+=row_h+1; } } diff --git a/src/map.c b/src/map.c index 72da245..2cec9d3 100644 --- a/src/map.c +++ b/src/map.c @@ -107,7 +107,6 @@ static bool ray_intersects_with_ground(vec3f begin, vec3f end) { diry /= length; dirz /= length; double nr_tiles_to_check = length*4; // check 4 points per tile. - for (int i = 1; i < nr_tiles_to_check; i++) { float xtocheck = begin.x + (dirx*i/4); float ytocheck = begin.y + (diry*i/4); @@ -147,8 +146,9 @@ void load_mapdata_into_world() { // Load lightmap for (int y = 0; y < MAP_SIZE_Y; y++) { for (int x = 0; x < MAP_SIZE_X; x++) { - for (int l = 0; l < 1; l++) { + for (int l = 0; l < MAX_LIGHT_EMITTERS; l++) { light_emitter emitter = loaded_map.light_emitters[l]; + if (!emitter.active) continue; float dist_tl = distance_between_3f(emitter.position, (vec3f){x, y, loaded_map.heightmap[y][x].topleft}); float dist_tr = distance_between_3f(emitter.position, (vec3f){x, y, loaded_map.heightmap[y][x].topright}); float dist_bl = distance_between_3f(emitter.position, (vec3f){x, y, loaded_map.heightmap[y][x].bottomleft}); @@ -158,29 +158,37 @@ void load_mapdata_into_world() { float p_tr = 1.0f - dist_tr / emitter.range; float p_bl = 1.0f - dist_bl / emitter.range; float p_br = 1.0f - dist_br / emitter.range; - - if (p_tl > 1.0f) p_tl = 1.0f; if (p_tl < 0.0f) p_tl = 0.0f; - if (p_tr > 1.0f) p_tr = 1.0f; if (p_tr < 0.0f) p_tr = 0.0f; - if (p_bl > 1.0f) p_bl = 1.0f; if (p_bl < 0.0f) p_bl = 0.0f; - if (p_br > 1.0f) p_br = 1.0f; if (p_br < 0.0f) p_br = 0.0f; - - loaded_map.lightmap[y][x].tl = p_tl; - loaded_map.lightmap[y][x].tr = p_tr; - loaded_map.lightmap[y][x].bl = p_bl; - loaded_map.lightmap[y][x].br = p_br; + if (p_tl < 0.0f) p_tl = 0.0f; + if (p_tr < 0.0f) p_tr = 0.0f; + if (p_bl < 0.0f) p_bl = 0.0f; + if (p_br < 0.0f) p_br = 0.0f; if (ray_intersects_with_ground((vec3f){x, y, loaded_map.heightmap[y][x].topleft}, emitter.position)) { - loaded_map.lightmap[y][x].tl = 0.0f; + p_tl = 0.0f; } if (ray_intersects_with_ground((vec3f){x, y, loaded_map.heightmap[y][x].topright}, emitter.position)) { - loaded_map.lightmap[y][x].tr = 0.0f; + p_tr = 0.0f; } if (ray_intersects_with_ground((vec3f){x, y, loaded_map.heightmap[y][x].bottomleft}, emitter.position)) { - loaded_map.lightmap[y][x].bl = 0.0f; + p_bl = 0.0f; } if (ray_intersects_with_ground((vec3f){x, y, loaded_map.heightmap[y][x].bottomright}, emitter.position)) { - loaded_map.lightmap[y][x].br = 0.0f; + p_br = 0.0f; } + + p_tl += loaded_map.lightmap[y][x].tl; + p_tr += loaded_map.lightmap[y][x].tr; + p_bl += loaded_map.lightmap[y][x].bl; + p_br += loaded_map.lightmap[y][x].br; + if (p_tl > 1.0f) p_tl = 1.0f; + if (p_tr > 1.0f) p_tr = 1.0f; + if (p_bl > 1.0f) p_bl = 1.0f; + if (p_br > 1.0f) p_br = 1.0f; + + loaded_map.lightmap[y][x].tl = p_tl; + loaded_map.lightmap[y][x].tr = p_tr; + loaded_map.lightmap[y][x].bl = p_bl; + loaded_map.lightmap[y][x].br = p_br; } } } @@ -205,7 +213,8 @@ void create_empty_map() { map_to_load.objects[2] = (object){.active = true, .h = 0, .position = (vec2f){0, 1}, .size = (vec3f){1,1,2}, .type = OBJECT_COBBLESTONEWALL1}; map_to_load.objects[3] = (object){.active = true, .h = 0, .position = (vec2f){0, 2}, .size = (vec3f){1,1,2}, .type = OBJECT_COBBLESTONEWALL1}; - map_to_load.light_emitters[0] = (light_emitter){.brightness = 1.0f, .position = (vec3f){0, 0, 0}, .range = 10.0f, .active = true}; + map_to_load.light_emitters[0] = (light_emitter){.brightness = 1.0f, .position = (vec3f){0, 0, 10}, .range = 20.0f, .active = true}; + map_to_load.light_emitters[1] = (light_emitter){.brightness = 1.0f, .position = (vec3f){0, 30, 10}, .range = 20.0f, .active = true}; load_mapdata_into_world(); } @@ -284,6 +293,22 @@ image* get_image_from_tiletype(tile_type tile) { } } +vec2 screen_pos_to_world_pos(platform_window* window, float x, float y) { + map_info info = get_map_info(window); + int tile_y = (y + _global_camera.y) / info.tile_height; + int tile_x = (((x + _global_camera.x) - (info.px_incline * tile_y)) / info.tile_width); + return (vec2){.x = (s32)tile_x, .y = (s32)tile_y}; +} + +vec2f world_pos_to_screen_pos(platform_window* window, float x, float y, float z) { + map_info info = get_map_info(window); + float xdiff_between_bottom_and_top = info.px_incline; + float render_x = (info.tile_width * x) + (xdiff_between_bottom_and_top * y); + float render_y = info.tile_height * y; + render_y -= z*info.px_raised_per_h; + return (vec2f){.x = render_x, .y = render_y}; +} + static float offset = 0.0f; static bool dir = true; void draw_grid(platform_window* window) { -- cgit v1.2.3-70-g09d2