Now layout build works fine

This commit is contained in:
2024-12-01 04:12:25 +03:00
parent 34bded906c
commit 42dc811cf6
28 changed files with 4040 additions and 2640 deletions

View File

@@ -1,9 +1,12 @@
all: graph_test entity_test rect2i regions
LIBS=
SDL_LIBS=$(shell pkg-config sdl2 --libs)
SDL_CFLAGS=$(shell pkg-config sdl2 --cflags)
IMGUI_OBJS= ./imgui/imgui.o ./imgui/imgui_draw.o ./imgui/imgui_tables.o ./imgui/imgui_widgets.o ./imgui/imgui_impl_sdlrenderer2.o ./imgui/imgui_impl_sdl2.o
# LIBS= ./godot_mockery/libgodot-mockery.a
CFLAGS = -I../src/flecs/distr -g
CXXFLAGS = -I../src/flecs/distr -I../src/modules/stream/ui -I./godot_mockery -I../src/godot -I../src/godot/platform/x11 -I. -I../src/modules/stream -g
CFLAGS = -I../src/flecs/distr $(SDL_CFLAGS) -I./cglm/include -I./flecs-game/include -g
CXXFLAGS = -I../src/flecs/distr -I../src/modules/stream/ui -I./godot_mockery -I../src/godot -I../src/godot/platform/x11 -I. -I../src/modules/stream -I./imgui $(SDL_CFLAGS) -g
#CXXFLAGS = -I../src/flecs/distr -I../src/modules/stream/ui -I../src/godot -I../src/godot/platform/x11 -I. -I../src/modules/stream -g
graph_test: graph_module.o flecs.o $(LIBS)
@@ -20,8 +23,14 @@ rect2i: rect2i.o $(LIBS)
region_tree.o: ../src/modules/stream/ui/region_tree.cpp
$(CC) $(CFLAGS) $(CXXFLAGS) -DTESTS -o $@ -c $<
regions: regions.o flecs.o region_tree.o godot_mockery/core/os/memory.o godot_mockery/core/ustring.o $(LIBS)
$(CXX) -o $@ regions.o flecs.o region_tree.o godot_mockery/core/os/memory.o godot_mockery/core/ustring.o $(LIBS)
#graph_module_growth.o: ../src/modules/stream/ui/graph_module_growth.cpp
# $(CC) $(CFLAGS) $(CXXFLAGS) -DTESTS -o $@ -c $<
#
#graph_module_core.o: ../src/modules/stream/ui/graph_module.cpp
# $(CC) $(CFLAGS) $(CXXFLAGS) -DTESTS -o $@ -c $<
regions: regions.o flecs.o region_tree.o godot_mockery/core/os/memory.o godot_mockery/core/ustring.o $(SDL_LIBS) $(IMGUI_OBJS) $(LIBS)
$(CXX) -o $@ regions.o flecs.o region_tree.o godot_mockery/core/os/memory.o godot_mockery/core/ustring.o $(IMGUI_OBJS) $(LIBS) $(SDL_LIBS)
MOCK_OBJS=godot_mockery/core/os/memory.o godot_mockery/core/error_macros.o \
godot_mockery/core/ustring.o godot_mockery/core/array.o \
@@ -58,7 +67,7 @@ godot_mockery/core/math/%.o: ../src/godot/core/math/%.cpp
$(CC) $(CFLAGS) $(CXXFLAGS) -DTESTS -o $@ -c $<
clean:
rm -f godot_mockery/core/*.o godot_mockery/core/os/*.o *.o graph_test entity_test rect2i regions
rm -f godot_mockery/core/*.o godot_mockery/core/os/*.o *.o graph_test entity_test rect2i regions $(FLECS_OBJS)
.PHONY: all clean

View File

@@ -460,4 +460,72 @@ String String::num_int64(int64_t p_num, int base, bool capitalize_hex) {
return s;
}
uint32_t String::hash(const char *p_cstr) {
uint32_t hashv = 5381;
uint32_t c;
while ((c = *p_cstr++)) {
hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
}
return hashv;
}
uint32_t String::hash(const char *p_cstr, int p_len) {
uint32_t hashv = 5381;
for (int i = 0; i < p_len; i++) {
hashv = ((hashv << 5) + hashv) + p_cstr[i]; /* hash * 33 + c */
}
return hashv;
}
uint32_t String::hash(const CharType *p_cstr, int p_len) {
uint32_t hashv = 5381;
for (int i = 0; i < p_len; i++) {
hashv = ((hashv << 5) + hashv) + p_cstr[i]; /* hash * 33 + c */
}
return hashv;
}
uint32_t String::hash(const CharType *p_cstr) {
uint32_t hashv = 5381;
uint32_t c;
while ((c = *p_cstr++)) {
hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
}
return hashv;
}
uint32_t String::hash() const {
/* simple djb2 hashing */
const CharType *chr = c_str();
uint32_t hashv = 5381;
uint32_t c;
while ((c = *chr++)) {
hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
}
return hashv;
}
uint64_t String::hash64() const {
/* simple djb2 hashing */
const CharType *chr = c_str();
uint64_t hashv = 5381;
uint64_t c;
while ((c = *chr++)) {
hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
}
return hashv;
}

View File

@@ -1,18 +1,45 @@
#include "../src/modules/stream/ui/region_tree.h"
#include "../src/modules/stream/ui/grid_misc.h"
#include "../src/modules/stream/world_editor.h"
#include "../src/modules/stream/ui/queries.h"
#include "../src/modules/stream/ui/building_layout_graph.h"
#include "imgui.h"
#include "imgui_impl_sdl2.h"
#include "imgui_impl_sdlrenderer2.h"
#include <SDL.h>
#include <SDL_opengl.h>
using graph_module = BuildingLayoutGraph::graph_module;
int main()
{
int i;
SDL_Window *window = NULL;
SDL_Surface *screenSurface = NULL;
SDL_Renderer *renderer;
SDL_WindowFlags window_flags;
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
HashMap<String, Color> colors;
make_random r(172);
flecs::world ecs;
ecs.component<WorldEditor::components::buildings_layout_grid_floor>();
ecs.component<WorldEditor::components::buildings_layout_grid_cell>();
flecs::log::set_level(1);
flecs::entity graph_base_e = ecs.entity("graph");
flecs::entity graph_e = ecs.entity("v1").child_of(graph_base_e);
flecs::entity grid_base_e = ecs.entity("grid");
flecs::entity grid_e = ecs.entity("v1").child_of(grid_base_e);
flecs::entity grid_floor_e = ecs.entity("floor_0").child_of(grid_e);
int grid_size = 27;
grid_floor_e.set<WorldEditor::components::buildings_layout_grid_floor>({.grid_size = grid_size, .size_left = grid_size * grid_size});
List<struct region> region_list;
struct region region;
region.region_et = graph_e.id();
region.rect = RegionRect2i(0, 0, 25, 25);
region.rect = RegionRect2i(1, 1, grid_size - 2, grid_size - 2);
region.complete = true;
region.can_grow = false;
region.can_grow_square = false;
struct region_tree regions;
regions.region = region;
regions.dump(grid_floor_e);
@@ -20,9 +47,32 @@ int main()
zone2_e = ecs.entity("zone2").child_of(graph_e),
zone3_e = ecs.entity("zone3").child_of(graph_e);
struct region region_data[] = {
{.region_et = zone1_e.id(), .rect = {0, 0, 1, 1}},
{.region_et = zone2_e.id(), .rect = {2, 2, 1, 1}},
{.region_et = zone3_e.id(), .rect = {4, 4, 1 ,1}}
{
.parent = graph_e.id(),
.region_et = zone1_e.id(),
.rect = {4, 4, 1, 1},
.remains_area = 140,
.can_grow_square = true,
.can_grow = true,
.complete = false
},
{
.parent = graph_e.id(),
.region_et = zone2_e.id(),
.rect = {4, 9, 1, 1},
.remains_area = 80,
.can_grow_square = true,
.can_grow = true,
.complete = false},
{
.parent = graph_e.id(),
.region_et = zone3_e.id(),
.rect = {20, 15, 1 ,1},
.remains_area = 50,
.can_grow_square = true,
.can_grow = true,
.complete = false
}
};
for (i = 0; i < sizeof(region_data) / sizeof(region_data[0]); i++)
region_list.push_back(region_data[i]);
@@ -30,6 +80,109 @@ int main()
regions.dump(grid_floor_e);
regions.grow();
regions.dump(grid_floor_e);
regions.place(grid_floor_e);
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) < 0)
goto out;
#ifdef SDL_HINT_IME_SHOW_UI
SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");
#endif
window_flags = (SDL_WindowFlags)(SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
window = SDL_CreateWindow("RegionTest", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 1024, window_flags);
if (window == NULL)
goto out;
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);
if (!renderer)
goto out;
// screenSurface = SDL_GetWindowSurface(window);
// if (!screenSurface)
// goto out;
IMGUI_CHECKVERSION();
ImGui::CreateContext();
{
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
}
// Setup Dear ImGui style
ImGui::StyleColorsDark();
//ImGui::StyleColorsLight();
// Setup Platform/Renderer backends
ImGui_ImplSDL2_InitForSDLRenderer(window, renderer);
ImGui_ImplSDLRenderer2_Init(renderer);
// SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF));
// SDL_UpdateWindowSurface(window);
SDL_Event event;
while(1) {
SDL_PollEvent(&event);
ImGui_ImplSDL2_ProcessEvent(&event);
switch(event.type) {
case SDL_QUIT:
goto shutdown;
break;
case SDL_WINDOWEVENT:
if (event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window))
goto shutdown;
break;
default:
break;
}
ecs.progress();
if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) {
SDL_Delay(10);
continue;
}
ImGui_ImplSDLRenderer2_NewFrame();
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();
{
ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
ImGui::End();
}
ImGui::Render();
{
ImGuiIO& io = ImGui::GetIO(); (void)io;
SDL_RenderSetScale(renderer, io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y);
}
SDL_SetRenderDrawColor(renderer, (Uint8)(clear_color.x * 255), (Uint8)(clear_color.y * 255), (Uint8)(clear_color.z * 255), (Uint8)(clear_color.w * 255));
SDL_RenderClear(renderer);
ecs.query_builder<const WorldEditor::components::buildings_layout_grid_cell>()
.build().each([grid_size, renderer, &colors, &r](flecs::entity e, const WorldEditor::components::buildings_layout_grid_cell &cell)
{
// flecs::log::warn("cell: %s: %s: %d", e.path().c_str(), cell.type.ascii().ptr(), cell.index);
int x = cell.index % grid_size;
int y = cell.index / grid_size;
int px = 1280 * x / grid_size;
int py = 1024 * y / grid_size;
int w = 1280 / grid_size;
int h = 1024 / grid_size;
// flecs::log::warn("position: %d %d", x, y);
if (!colors.has(cell.type))
colors[cell.type] = Color((float)(r.get() % 256) / 256.0f, (float)(r.get() % 256) / 256.0f, (float)(r.get() % 256) / 256.0f, 1.0f);
const Color &c = colors[cell.type];
SDL_SetRenderDrawColor(renderer, (uint8_t)(c.r * 255), (uint8_t)(c.g * 255), (uint8_t)(c.b * 255), 255);
SDL_Rect rect = {px, py, w, h};
SDL_RenderFillRect(renderer, &rect);
SDL_SetRenderDrawColor(renderer, 255, 15, 15, 255);
SDL_RenderDrawRect(renderer, &rect);
});
ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData(), renderer);
SDL_RenderPresent(renderer);
}
shutdown:
ImGui_ImplSDLRenderer2_Shutdown();
ImGui_ImplSDL2_Shutdown();
ImGui::DestroyContext();
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
out:
return 0;
}