Working on rendering
This commit is contained in:
@@ -1,16 +1,23 @@
|
||||
#define BASIC_IMPL
|
||||
#include "../pdf_browser/basic.h"
|
||||
#include "../basic/basic.h"
|
||||
#include "raylib.h"
|
||||
#include "raymath.h"
|
||||
|
||||
#include "buffer.cpp"
|
||||
|
||||
Arena FrameArena;
|
||||
|
||||
using Vec2 = Vector2;
|
||||
struct Rect2 {
|
||||
Vec2 min;
|
||||
Vec2 max;
|
||||
};
|
||||
|
||||
Vec2 GetSize(Rect2 r) {
|
||||
Vec2 result = {r.max.x - r.min.x, r.max.y - r.min.y};
|
||||
return result;
|
||||
}
|
||||
|
||||
Rectangle ToRectangle(Rect2 r) {
|
||||
Rectangle result = {r.min.x, r.min.y, r.max.x - r.min.x, r.max.y - r.min.y};
|
||||
return result;
|
||||
@@ -19,12 +26,21 @@ Rectangle ToRectangle(Rect2 r) {
|
||||
// Render units - positions ready to draw, y
|
||||
// World units - positions offset by screen movement
|
||||
// Window units - positions inside the window (starts in left top of window)
|
||||
// World window units
|
||||
// WindowWorld units - positions offset by a position inside the buffer
|
||||
|
||||
struct Window {
|
||||
Rect2 rect_in_world_units;
|
||||
Vec2 window_world_to_window_units;
|
||||
|
||||
Array<Range> cursors;
|
||||
Buffer buffer;
|
||||
};
|
||||
|
||||
Vec2 WindowWorldToWindowUnits(Vec2 value, const Window &window) {
|
||||
Vec2 result = Vector2Subtract(value, window.window_world_to_window_units);
|
||||
return result;
|
||||
}
|
||||
|
||||
Vec2 WorldToRenderUnits(Vec2 value, Vec2 camera_offset_world_to_render_units) {
|
||||
Vec2 result = Vector2Subtract(value, camera_offset_world_to_render_units);
|
||||
return result;
|
||||
@@ -47,15 +63,22 @@ int main() {
|
||||
InitScratch();
|
||||
RunBufferTests();
|
||||
|
||||
return 0;
|
||||
InitWindow(800, 600, "Hello");
|
||||
SetTargetFPS(60);
|
||||
|
||||
InitArena(&FrameArena);
|
||||
float font_size = 14;
|
||||
float font_spacing = 1;
|
||||
Font font = LoadFontEx("C:/Windows/Fonts/consola.ttf", (int)font_size, NULL, 250);
|
||||
|
||||
Array<char> buffer = {};
|
||||
Buffer buffer = {};
|
||||
InitBuffer(&buffer);
|
||||
for (int i = 0; i < 100; i += 1) {
|
||||
Array<Edit> edits = {FrameArena};
|
||||
AddEdit(&edits, GetEnd(buffer), Format(FrameArena, "line number: %d\n", i));
|
||||
ApplyEdits(&buffer, edits);
|
||||
}
|
||||
|
||||
Array<Window> windows = {};
|
||||
windows.add({GetScreenRectRenderUnits()});
|
||||
|
||||
@@ -64,38 +87,80 @@ int main() {
|
||||
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT) || IsKeyDown(KEY_SPACE)) {
|
||||
camera_offset_world_to_render_units = Vector2Subtract(camera_offset_world_to_render_units, GetMouseDelta());
|
||||
}
|
||||
{
|
||||
Window *focused_window = &windows[0];
|
||||
float mouse_wheel = GetMouseWheelMove() * 48;
|
||||
focused_window->window_world_to_window_units.y -= mouse_wheel;
|
||||
}
|
||||
|
||||
for (int c = GetCharPressed(); c; c = GetCharPressed()) {
|
||||
Assert(c >= ' ' && c <= '~');
|
||||
buffer.add((char)c);
|
||||
String string = "?";
|
||||
UTF8Result utf8 = UTF32ToUTF8((uint32_t)c);
|
||||
if (utf8.error == 0) {
|
||||
string = {(char *)utf8.out_str, (int64_t)utf8.len};
|
||||
}
|
||||
|
||||
Array<Edit> edits = {FrameArena};
|
||||
AddEdit(&edits, {}, string);
|
||||
ApplyEdits(&buffer, edits);
|
||||
}
|
||||
|
||||
BeginDrawing();
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
For(windows) {
|
||||
Rect2 rect_in_render_units = {
|
||||
Rect2 window_rect_in_render_units = {
|
||||
WorldToRenderUnits(it.rect_in_world_units.min, camera_offset_world_to_render_units),
|
||||
WorldToRenderUnits(it.rect_in_world_units.max, camera_offset_world_to_render_units),
|
||||
};
|
||||
Rectangle rectangle_in_render_units = ToRectangle(rect_in_render_units);
|
||||
Rectangle rectangle_in_render_units = ToRectangle(window_rect_in_render_units);
|
||||
DrawRectangleRec(rectangle_in_render_units, WHITE);
|
||||
|
||||
{
|
||||
Vec2 text_position_in_window_units = {};
|
||||
Vec2 text_position_in_world_units = WindowToWorldUnits(text_position_in_window_units, it);
|
||||
Vec2 text_position_in_render_units = WorldToRenderUnits(text_position_in_world_units, camera_offset_world_to_render_units);
|
||||
DrawTextEx(font, "window 1", text_position_in_render_units, font_size, font_spacing, BLACK);
|
||||
}
|
||||
// @todo: add window bar?
|
||||
// {
|
||||
// Vec2 text_position_in_window_units = {};
|
||||
// Vec2 text_position_in_world_units = WindowToWorldUnits(text_position_in_window_units, it);
|
||||
// Vec2 text_position_in_render_units = WorldToRenderUnits(text_position_in_world_units, camera_offset_world_to_render_units);
|
||||
// DrawTextEx(font, "window 1", text_position_in_render_units, font_size, font_spacing, BLACK);
|
||||
// }
|
||||
|
||||
//
|
||||
// Line rendering
|
||||
//
|
||||
{
|
||||
Vec2 text_position_in_window_units = {0, font_size};
|
||||
Vec2 text_position_in_world_units = WindowToWorldUnits(text_position_in_window_units, it);
|
||||
Vec2 text_position_in_render_units = WorldToRenderUnits(text_position_in_world_units, camera_offset_world_to_render_units);
|
||||
Rect2 screen_rect_in_render_units = GetScreenRectRenderUnits();
|
||||
Rect2 window_rect_in_render_units_clamped_to_screen = window_rect_in_render_units;
|
||||
window_rect_in_render_units_clamped_to_screen.min.x = Clamp(window_rect_in_render_units_clamped_to_screen.min.x, screen_rect_in_render_units.min.x, screen_rect_in_render_units.max.x);
|
||||
window_rect_in_render_units_clamped_to_screen.max.x = Clamp(window_rect_in_render_units_clamped_to_screen.max.x, screen_rect_in_render_units.min.x, screen_rect_in_render_units.max.x);
|
||||
window_rect_in_render_units_clamped_to_screen.min.y = Clamp(window_rect_in_render_units_clamped_to_screen.min.y, screen_rect_in_render_units.min.y, screen_rect_in_render_units.max.y);
|
||||
window_rect_in_render_units_clamped_to_screen.max.y = Clamp(window_rect_in_render_units_clamped_to_screen.max.y, screen_rect_in_render_units.min.y, screen_rect_in_render_units.max.y);
|
||||
|
||||
buffer.add('\0');
|
||||
DrawTextEx(font, buffer.data, text_position_in_render_units, font_size, font_spacing, BLACK);
|
||||
buffer.pop();
|
||||
Vec2 s = GetSize(window_rect_in_render_units_clamped_to_screen);
|
||||
|
||||
// buffer_pixel_size := GetRectSize(text_window_rect);
|
||||
// _miny := w.scroll.y / Monosize.y;
|
||||
// _maxy := (w.scroll.y + buffer_pixel_size.y) / Monosize.y;
|
||||
|
||||
// _minx := w.scroll.x / Monosize.x;
|
||||
// _maxx := (w.scroll.x + buffer_pixel_size.x) / Monosize.x;
|
||||
|
||||
// miny := :int(floorf(_miny));
|
||||
// minx := :int(floorf(_minx));
|
||||
|
||||
// maxy := :int(ceilf(_maxy));
|
||||
// maxx := :int(ceilf(_maxx));
|
||||
|
||||
float y = 0;
|
||||
ForItem(line_range, buffer.lines) {
|
||||
Vec2 text_position_in_world_window_units = {0, y};
|
||||
y += font_size;
|
||||
Vec2 text_position_in_window_units = WindowWorldToWindowUnits(text_position_in_world_window_units, it);
|
||||
Vec2 text_position_in_world_units = WindowToWorldUnits(text_position_in_window_units, it);
|
||||
Vec2 text_position_in_render_units = WorldToRenderUnits(text_position_in_world_units, camera_offset_world_to_render_units);
|
||||
|
||||
String string = CopyNullTerminated(FrameArena, buffer, line_range);
|
||||
DrawTextEx(font, string.data, text_position_in_render_units, font_size, font_spacing, BLACK);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user