Stop using WindowSize and actually make use of event window size, provide default

This commit is contained in:
Krzosa Karol
2024-08-15 15:09:14 +02:00
parent d0bc7bdc4a
commit bafe0af407
6 changed files with 39 additions and 30 deletions

View File

@@ -110,6 +110,11 @@ Vec2 operator/=(Vec2 &a, float b) { a = a / b; return a; }
// clang-format on // clang-format on
Rect2 Rect0Size(float x, float y) {
Rect2 rect = {0, 0, x, y};
return rect;
}
Rect2 Rect2FromSize(Vec2 pos, Vec2 size) { Rect2 Rect2FromSize(Vec2 pos, Vec2 size) {
Rect2 result = {}; Rect2 result = {};
result.min = pos; result.min = pos;

View File

@@ -73,6 +73,11 @@ Vec2I operator/=(Vec2I &a, Int b) { a = a / b; return a; }
// clang-format on // clang-format on
Rect2I RectI0Size(Int x, Int y) {
Rect2I rect = {0, 0, x, y};
return rect;
}
Rect2I Rect2IFromSize(Vec2I pos, Vec2I size) { Rect2I Rect2IFromSize(Vec2I pos, Vec2I size) {
Rect2I result = {}; Rect2I result = {};
result.min = pos; result.min = pos;

View File

@@ -29,46 +29,36 @@ int64_t TotalVertexCount;
unsigned VBO, VAO; unsigned VBO, VAO;
Shader Shader2D; Shader Shader2D;
Arena RenderArena; Arena RenderArena;
Vec2 WindowSize;
Rect2 CurrentScissor; Rect2 CurrentScissor;
Font MainFont; Font MainFont;
Int FontLineSpacing; Int FontLineSpacing;
Int FontCharSpacing; Int FontCharSpacing;
Rect2 GetScreenRectF() { void BeginFrameRender(float wx, float wy) {
Rect2 result = {0, 0, WindowSize.x, WindowSize.y};
return result;
}
Rect2I GetScreenRectI() {
Rect2I result = {0, 0, (Int)WindowSize.x, (Int)WindowSize.y};
return result;
}
void BeginFrameRender() {
Clear(&RenderArena); Clear(&RenderArena);
TotalVertexCount = 0; TotalVertexCount = 0;
Vertices.first = NULL; Vertices.first = NULL;
Vertices.last = NULL; Vertices.last = NULL;
CurrentScissor = GetScreenRectF(); CurrentScissor = Rect0Size(wx, wy);
} }
void EndFrameRender(Color color) { void EndFrameRender(float wx, float wy, Color color) {
glEnable(GL_BLEND); glEnable(GL_BLEND);
glEnable(GL_SCISSOR_TEST); glEnable(GL_SCISSOR_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE); glDisable(GL_CULL_FACE);
glViewport(0, 0, (GLsizei)WindowSize.x, (GLsizei)WindowSize.y); glViewport(0, 0, (GLsizei)wx, (GLsizei)wy);
glScissor(0, 0, (GLsizei)WindowSize.x, (GLsizei)WindowSize.y); glScissor(0, 0, (GLsizei)wx, (GLsizei)wy);
glClearColor(color.r, color.g, color.b, color.a); glClearColor(color.r, color.g, color.b, color.a);
glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
// Default draw using the font texture // Default draw using the font texture
glBindProgramPipeline(Shader2D.pipeline); glBindProgramPipeline(Shader2D.pipeline);
float xinverse = 1.f / (WindowSize.x / 2.f); float xinverse = 1.f / (wx / 2.f);
float yinverse = 1.f / (WindowSize.y / 2.f); float yinverse = 1.f / (wy / 2.f);
glProgramUniform2f(Shader2D.vshader, 0, xinverse, yinverse); glProgramUniform2f(Shader2D.vshader, 0, xinverse, yinverse);
for (VertexNode2D *it = Vertices.first; it; it = it->next) { for (VertexNode2D *it = Vertices.first; it; it = it->next) {
Rect2 rect = it->scissor; Rect2 rect = it->scissor;
@@ -76,7 +66,7 @@ void EndFrameRender(Color color) {
GLint y = (GLint)rect.min.y; GLint y = (GLint)rect.min.y;
GLsizei w = (GLsizei)(rect.max.x - x); GLsizei w = (GLsizei)(rect.max.x - x);
GLsizei h = (GLsizei)(rect.max.y - y); GLsizei h = (GLsizei)(rect.max.y - y);
glScissor(x, (GLint)WindowSize.y - (GLint)rect.max.y, w, h); glScissor(x, (GLint)wy - (GLint)rect.max.y, w, h);
glNamedBufferSubData(VBO, 0, it->count * sizeof(Vertex2D), it->vertices); glNamedBufferSubData(VBO, 0, it->count * sizeof(Vertex2D), it->vertices);
glBindVertexArray(VAO); glBindVertexArray(VAO);
GLint s_texture = 0; // texture unit that sampler2D will use in GLSL code GLint s_texture = 0; // texture unit that sampler2D will use in GLSL code

View File

@@ -155,8 +155,7 @@ Event TranslateSDLEvent(SDL_Event *input_event) {
} }
void Update(Event event) { void Update(Event event) {
WindowSize = {(float)event.xwindow, (float)event.ywindow}; LayoutWindows(event.xwindow, event.ywindow);
LayoutWindows();
Scratch scratch; Scratch scratch;
Array<Int> order = GetWindowZOrder(scratch); Array<Int> order = GetWindowZOrder(scratch);
@@ -330,6 +329,14 @@ int main(int argc, char **argv)
For(frame_events) { For(frame_events) {
Serialize(&ser, &it); Serialize(&ser, &it);
if (it.kind == EVENT_QUIT) goto end_of_editor_loop; if (it.kind == EVENT_QUIT) goto end_of_editor_loop;
if (it.xwindow == 0 || it.ywindow == 0) {
int xwindow, ywindow;
SDL_GetWindowSize(SDLWindow, &xwindow, &ywindow);
it.xwindow = xwindow;
it.ywindow = ywindow;
}
Update(it); Update(it);
} }
@@ -349,15 +356,16 @@ int main(int argc, char **argv)
SDL_SetWindowTitle(SDLWindow, string.data); SDL_SetWindowTitle(SDLWindow, string.data);
} }
LayoutWindows(); // This is here to render changes in title bar size without a frame of delay Event *event = GetLast(frame_events);
BeginFrameRender(); LayoutWindows(event->xwindow, event->ywindow); // This is here to render changes in title bar size without a frame of delay
BeginFrameRender(event->xwindow, event->ywindow);
Array<Int> order = GetWindowZOrder(scratch); Array<Int> order = GetWindowZOrder(scratch);
For(IterateInReverse(&order)) { For(IterateInReverse(&order)) {
Window *window = Windows[it].o; Window *window = Windows[it].o;
if (!window->visible) continue; if (!window->visible) continue;
DrawWindow(window, *GetLast(frame_events)); DrawWindow(window, *GetLast(frame_events));
} }
EndFrameRender(ColorBackground); EndFrameRender(event->xwindow, event->ywindow, ColorBackground);
SDL_GL_SwapWindow(SDLWindow); SDL_GL_SwapWindow(SDLWindow);
} }
end_of_editor_loop:; end_of_editor_loop:;

View File

@@ -166,9 +166,9 @@ void InitWindows() {
ActiveWindow = {0}; ActiveWindow = {0};
} }
void LayoutWindows() { void LayoutWindows(int16_t wx, int16_t wy) {
float ScrollBarSize = (10.f * DPIScale); float ScrollBarSize = (10.f * DPIScale);
Rect2I screen_rect = GetScreenRectI(); Rect2I screen_rect = RectI0Size(wx, wy);
float line_numbers_size = (float)FontCharSpacing * 10; float line_numbers_size = (float)FontCharSpacing * 10;
float sizex = (float)GetSize(screen_rect).x; float sizex = (float)GetSize(screen_rect).x;
@@ -203,7 +203,7 @@ void LayoutWindows() {
{ {
Window *window = GetWindow(DebugWindowID); Window *window = GetWindow(DebugWindowID);
Rect2 screen_rect = GetScreenRectF(); Rect2 screen_rect = Rect0Size(wx, wy);
Vec2 size = GetSize(screen_rect); Vec2 size = GetSize(screen_rect);
Rect2 a = CutLeft(&screen_rect, 0.3f * size.x); Rect2 a = CutLeft(&screen_rect, 0.3f * size.x);

View File

@@ -106,9 +106,10 @@ void DrawUnderline(Window *window, View *view, Buffer *buffer, Range range, Colo
} }
void DrawWindow(Window *window, Event &event) { void DrawWindow(Window *window, Event &event) {
View *view = GetView(window->active_view); View *view = GetView(window->active_view);
Buffer *buffer = GetBuffer(view->active_buffer); Buffer *buffer = GetBuffer(view->active_buffer);
SetScissor(GetScreenRectF()); Rect2 screen_rect = Rect0Size(event.xwindow, event.ywindow);
SetScissor(screen_rect);
bool is_active = window->id == ActiveWindow || window->title_bar_window == ActiveWindow; bool is_active = window->id == ActiveWindow || window->title_bar_window == ActiveWindow;
@@ -266,7 +267,7 @@ void DrawWindow(Window *window, Event &event) {
} }
if (!is_active) { if (!is_active) {
SetScissor(GetScreenRectF()); SetScissor(screen_rect);
DrawRect(window->total_rect, ColorInactiveWindow); DrawRect(window->total_rect, ColorInactiveWindow);
} }
} }