Basic multiline buffer concept
This commit is contained in:
@@ -1,17 +1,104 @@
|
||||
#define BASIC_IMPL
|
||||
#include "../pdf_browser/basic.h"
|
||||
|
||||
#include "raylib.h"
|
||||
#include "raymath.h"
|
||||
|
||||
#include "buffer.cpp"
|
||||
|
||||
using Vec2 = Vector2;
|
||||
struct Rect2 {
|
||||
Vec2 min;
|
||||
Vec2 max;
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// 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
|
||||
|
||||
struct Window {
|
||||
Rect2 rect_in_world_units;
|
||||
};
|
||||
|
||||
Vec2 WorldToRenderUnits(Vec2 value, Vec2 camera_offset_world_to_render_units) {
|
||||
Vec2 result = Vector2Subtract(value, camera_offset_world_to_render_units);
|
||||
return result;
|
||||
}
|
||||
|
||||
Vec2 WindowToWorldUnits(Vec2 value, const Window &window) {
|
||||
Vector2 result = Vector2Add(value, window.rect_in_world_units.min);
|
||||
return result;
|
||||
}
|
||||
|
||||
Rect2 GetScreenRectRenderUnits() {
|
||||
Rect2 result = {
|
||||
{ 0, 0},
|
||||
{(float)GetRenderWidth(), (float)GetRenderHeight()}
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
int main() {
|
||||
InitWindow(1280, 720, "Text editor");
|
||||
SetWindowState(FLAG_WINDOW_RESIZABLE);
|
||||
InitScratch();
|
||||
RunBufferTests();
|
||||
|
||||
return 0;
|
||||
InitWindow(800, 600, "Hello");
|
||||
SetTargetFPS(60);
|
||||
|
||||
float font_size = 14;
|
||||
float font_spacing = 1;
|
||||
Font font = LoadFontEx("C:/Windows/Fonts/consola.ttf", (int)font_size, NULL, 250);
|
||||
|
||||
Array<char> buffer = {};
|
||||
Array<Window> windows = {};
|
||||
windows.add({GetScreenRectRenderUnits()});
|
||||
|
||||
Vec2 camera_offset_world_to_render_units = {};
|
||||
while (!WindowShouldClose()) {
|
||||
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT) || IsKeyDown(KEY_SPACE)) {
|
||||
camera_offset_world_to_render_units = Vector2Subtract(camera_offset_world_to_render_units, GetMouseDelta());
|
||||
}
|
||||
|
||||
for (int c = GetCharPressed(); c; c = GetCharPressed()) {
|
||||
Assert(c >= ' ' && c <= '~');
|
||||
buffer.add((char)c);
|
||||
}
|
||||
|
||||
BeginDrawing();
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
For(windows) {
|
||||
Rect2 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);
|
||||
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);
|
||||
}
|
||||
|
||||
{
|
||||
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);
|
||||
|
||||
buffer.add('\0');
|
||||
DrawTextEx(font, buffer.data, text_position_in_render_units, font_size, font_spacing, BLACK);
|
||||
buffer.pop();
|
||||
}
|
||||
}
|
||||
|
||||
EndDrawing();
|
||||
}
|
||||
CloseWindow();
|
||||
}
|
||||
Reference in New Issue
Block a user