2023-09-07 12:51:48 +00:00
|
|
|
/*************************************************************************
|
2023-08-24 22:30:20 +00:00
|
|
|
*
|
2023-09-07 12:51:48 +00:00
|
|
|
* Orca
|
|
|
|
* Copyright 2023 Martin Fouilleul and the Orca project contributors
|
|
|
|
* See LICENSE.txt for licensing information
|
2023-08-24 22:30:20 +00:00
|
|
|
*
|
2023-09-07 12:51:48 +00:00
|
|
|
**************************************************************************/
|
2023-08-19 12:49:23 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define _USE_MATH_DEFINES //NOTE: necessary for MSVC
|
|
|
|
#include <math.h>
|
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
#include "orca.h"
|
2023-08-19 12:49:23 +00:00
|
|
|
|
|
|
|
#include "tiger.c"
|
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_font create_font()
|
2023-08-19 12:49:23 +00:00
|
|
|
{
|
|
|
|
//NOTE(martin): create font
|
2023-09-14 09:54:38 +00:00
|
|
|
oc_arena_scope scratch = oc_scratch_begin();
|
2023-09-13 16:10:47 +00:00
|
|
|
oc_str8 fontPath = oc_path_executable_relative(scratch.arena, OC_STR8("../../resources/OpenSansLatinSubset.ttf"));
|
|
|
|
char* fontPathCString = oc_str8_to_cstring(scratch.arena, fontPath);
|
2023-08-19 12:49:23 +00:00
|
|
|
|
|
|
|
FILE* fontFile = fopen(fontPathCString, "r");
|
|
|
|
if(!fontFile)
|
|
|
|
{
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_log_error("Could not load font file '%s': %s\n", fontPathCString, strerror(errno));
|
2023-09-13 16:10:47 +00:00
|
|
|
oc_scratch_end(scratch);
|
2023-08-24 22:30:20 +00:00
|
|
|
return (oc_font_nil());
|
2023-08-19 12:49:23 +00:00
|
|
|
}
|
|
|
|
unsigned char* fontData = 0;
|
|
|
|
fseek(fontFile, 0, SEEK_END);
|
|
|
|
u32 fontDataSize = ftell(fontFile);
|
|
|
|
rewind(fontFile);
|
|
|
|
fontData = (unsigned char*)malloc(fontDataSize);
|
|
|
|
fread(fontData, 1, fontDataSize, fontFile);
|
|
|
|
fclose(fontFile);
|
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_unicode_range ranges[5] = { OC_UNICODE_BASIC_LATIN,
|
|
|
|
OC_UNICODE_C1_CONTROLS_AND_LATIN_1_SUPPLEMENT,
|
|
|
|
OC_UNICODE_LATIN_EXTENDED_A,
|
|
|
|
OC_UNICODE_LATIN_EXTENDED_B,
|
|
|
|
OC_UNICODE_SPECIALS };
|
2023-08-19 12:49:23 +00:00
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_font font = oc_font_create_from_memory(oc_str8_from_buffer(fontDataSize, (char*)fontData), 5, ranges);
|
2023-08-19 12:49:23 +00:00
|
|
|
free(fontData);
|
2023-09-13 16:10:47 +00:00
|
|
|
oc_scratch_end(scratch);
|
2023-08-19 12:49:23 +00:00
|
|
|
return (font);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_init();
|
2023-08-19 12:49:23 +00:00
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_rect windowRect = { .x = 100, .y = 100, .w = 810, .h = 610 };
|
|
|
|
oc_window window = oc_window_create(windowRect, OC_STR8("test"), 0);
|
2023-08-19 12:49:23 +00:00
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_rect contentRect = oc_window_get_content_rect(window);
|
2023-08-19 12:49:23 +00:00
|
|
|
|
|
|
|
//NOTE: create surface
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_surface surface = oc_surface_create_for_window(window, OC_CANVAS);
|
|
|
|
if(oc_surface_is_nil(surface))
|
2023-08-19 12:49:23 +00:00
|
|
|
{
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_log_error("Couldn't create surface\n");
|
2023-08-19 12:49:23 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_surface_swap_interval(surface, 0);
|
2023-08-19 12:49:23 +00:00
|
|
|
|
|
|
|
//TODO: create canvas
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_canvas canvas = oc_canvas_create();
|
2023-08-19 12:49:23 +00:00
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
if(oc_canvas_is_nil(canvas))
|
2023-08-19 12:49:23 +00:00
|
|
|
{
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_log_error("Error: couldn't create canvas\n");
|
2023-08-19 12:49:23 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_font font = create_font();
|
2023-08-19 12:49:23 +00:00
|
|
|
|
|
|
|
// start app
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_window_bring_to_front(window);
|
|
|
|
oc_window_focus(window);
|
2023-08-19 12:49:23 +00:00
|
|
|
|
|
|
|
bool tracked = false;
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_vec2 trackPoint = { 0 };
|
2023-08-19 12:49:23 +00:00
|
|
|
|
|
|
|
f32 zoom = 1;
|
|
|
|
f32 startX = 300, startY = 200;
|
|
|
|
bool singlePath = false;
|
|
|
|
int singlePathIndex = 0;
|
|
|
|
|
|
|
|
f64 frameTime = 0;
|
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_input_state inputState = { 0 };
|
2023-08-19 12:49:23 +00:00
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
while(!oc_should_quit())
|
2023-08-19 12:49:23 +00:00
|
|
|
{
|
2023-09-14 09:54:38 +00:00
|
|
|
oc_arena_scope scratch = oc_scratch_begin();
|
2023-08-24 22:30:20 +00:00
|
|
|
f64 startTime = oc_clock_time(OC_CLOCK_MONOTONIC);
|
2023-08-19 12:49:23 +00:00
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_pump_events(0);
|
|
|
|
oc_event* event = 0;
|
2023-09-13 16:10:47 +00:00
|
|
|
while((event = oc_next_event(scratch.arena)) != 0)
|
2023-08-19 12:49:23 +00:00
|
|
|
{
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_input_process_event(&inputState, event);
|
2023-08-19 12:49:23 +00:00
|
|
|
|
|
|
|
switch(event->type)
|
|
|
|
{
|
2023-08-24 22:30:20 +00:00
|
|
|
case OC_EVENT_WINDOW_CLOSE:
|
2023-08-19 12:49:23 +00:00
|
|
|
{
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_request_quit();
|
2023-08-19 12:49:23 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
case OC_EVENT_MOUSE_BUTTON:
|
2023-08-19 12:49:23 +00:00
|
|
|
{
|
2023-08-24 22:30:20 +00:00
|
|
|
if(event->key.code == OC_MOUSE_LEFT)
|
2023-08-19 12:49:23 +00:00
|
|
|
{
|
2023-08-24 22:30:20 +00:00
|
|
|
if(event->key.action == OC_KEY_PRESS)
|
2023-08-19 12:49:23 +00:00
|
|
|
{
|
|
|
|
tracked = true;
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_vec2 mousePos = oc_mouse_position(&inputState);
|
2023-08-19 12:49:23 +00:00
|
|
|
trackPoint.x = (mousePos.x - startX) / zoom;
|
|
|
|
trackPoint.y = (mousePos.y - startY) / zoom;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tracked = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
case OC_EVENT_MOUSE_WHEEL:
|
2023-08-19 12:49:23 +00:00
|
|
|
{
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_vec2 mousePos = oc_mouse_position(&inputState);
|
2023-08-19 12:49:23 +00:00
|
|
|
f32 pinX = (mousePos.x - startX) / zoom;
|
|
|
|
f32 pinY = (mousePos.y - startY) / zoom;
|
|
|
|
|
|
|
|
zoom *= 1 + event->mouse.deltaY * 0.01;
|
2023-08-24 22:30:20 +00:00
|
|
|
zoom = oc_clamp(zoom, 0.5, 5);
|
2023-08-19 12:49:23 +00:00
|
|
|
|
|
|
|
startX = mousePos.x - pinX * zoom;
|
|
|
|
startY = mousePos.y - pinY * zoom;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
case OC_EVENT_KEYBOARD_KEY:
|
2023-08-19 12:49:23 +00:00
|
|
|
{
|
2023-08-24 22:30:20 +00:00
|
|
|
if(event->key.action == OC_KEY_PRESS || event->key.action == OC_KEY_REPEAT)
|
2023-08-19 12:49:23 +00:00
|
|
|
{
|
|
|
|
switch(event->key.code)
|
|
|
|
{
|
2023-08-24 22:30:20 +00:00
|
|
|
case OC_KEY_SPACE:
|
2023-08-19 12:49:23 +00:00
|
|
|
singlePath = !singlePath;
|
|
|
|
break;
|
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
case OC_KEY_UP:
|
2023-08-19 12:49:23 +00:00
|
|
|
{
|
2023-08-24 22:30:20 +00:00
|
|
|
if(event->key.mods & OC_KEYMOD_SHIFT)
|
2023-08-19 12:49:23 +00:00
|
|
|
{
|
|
|
|
singlePathIndex++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
zoom += 0.001;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
case OC_KEY_DOWN:
|
2023-08-19 12:49:23 +00:00
|
|
|
{
|
2023-08-24 22:30:20 +00:00
|
|
|
if(event->key.mods & OC_KEYMOD_SHIFT)
|
2023-08-19 12:49:23 +00:00
|
|
|
{
|
|
|
|
singlePathIndex--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
zoom -= 0.001;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(tracked)
|
|
|
|
{
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_vec2 mousePos = oc_mouse_position(&inputState);
|
2023-08-19 12:49:23 +00:00
|
|
|
startX = mousePos.x - trackPoint.x * zoom;
|
|
|
|
startY = mousePos.y - trackPoint.y * zoom;
|
|
|
|
}
|
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_surface_select(surface);
|
2023-08-19 12:49:23 +00:00
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_set_color_rgba(1, 0, 1, 1);
|
|
|
|
oc_clear();
|
2023-08-19 12:49:23 +00:00
|
|
|
|
2023-09-11 18:02:46 +00:00
|
|
|
oc_matrix_multiply_push((oc_mat2x3){ zoom, 0, startX,
|
2023-09-13 16:10:47 +00:00
|
|
|
0, zoom, startY });
|
2023-08-19 12:49:23 +00:00
|
|
|
|
|
|
|
draw_tiger(singlePath, singlePathIndex);
|
|
|
|
|
|
|
|
if(singlePath)
|
|
|
|
{
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_log_info("display single path %i\n", singlePathIndex);
|
|
|
|
oc_log_info("viewpos = (%f, %f), zoom = %f\n", startX, startY, zoom);
|
2023-08-19 12:49:23 +00:00
|
|
|
}
|
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_matrix_pop();
|
2023-08-19 12:49:23 +00:00
|
|
|
|
|
|
|
// text
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_set_color_rgba(0, 0, 1, 1);
|
|
|
|
oc_set_font(font);
|
|
|
|
oc_set_font_size(12);
|
|
|
|
oc_move_to(50, 600 - 50);
|
|
|
|
|
2023-09-13 16:10:47 +00:00
|
|
|
oc_str8 text = oc_str8_pushf(scratch.arena,
|
2023-08-24 22:30:20 +00:00
|
|
|
"Orca vector graphics test program (frame time = %fs, fps = %f)...",
|
|
|
|
frameTime,
|
|
|
|
1. / frameTime);
|
|
|
|
oc_text_outlines(text);
|
|
|
|
oc_fill();
|
|
|
|
|
|
|
|
oc_log_info("Orca vector graphics test program (frame time = %fs, fps = %f)...\n",
|
|
|
|
frameTime,
|
|
|
|
1. / frameTime);
|
|
|
|
|
2023-09-10 13:21:11 +00:00
|
|
|
oc_render(canvas);
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_surface_present(surface);
|
|
|
|
|
|
|
|
oc_input_next_frame(&inputState);
|
2023-09-13 16:10:47 +00:00
|
|
|
|
|
|
|
oc_scratch_end(scratch);
|
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
frameTime = oc_clock_time(OC_CLOCK_MONOTONIC) - startTime;
|
2023-08-19 12:49:23 +00:00
|
|
|
}
|
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_font_destroy(font);
|
|
|
|
oc_canvas_destroy(canvas);
|
|
|
|
oc_surface_destroy(surface);
|
|
|
|
oc_window_destroy(window);
|
2023-08-19 12:49:23 +00:00
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_terminate();
|
2023-08-19 12:49:23 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|