orca/sketches/tiger/main.c

257 lines
7.6 KiB
C
Raw Normal View History

2023-09-07 12:51:48 +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-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>
#include "orca.h"
2023-08-19 12:49:23 +00:00
#include "tiger.c"
oc_font create_font()
2023-08-19 12:49:23 +00:00
{
//NOTE(martin): create font
oc_arena_scope scratch = oc_scratch_begin();
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)
{
oc_log_error("Could not load font file '%s': %s\n", fontPathCString, strerror(errno));
oc_scratch_end(scratch);
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);
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
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);
oc_scratch_end(scratch);
2023-08-19 12:49:23 +00:00
return (font);
}
int main()
{
oc_init();
2023-08-19 12:49:23 +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
oc_rect contentRect = oc_window_get_content_rect(window);
2023-08-19 12:49:23 +00:00
//NOTE: create surface
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
{
oc_log_error("Couldn't create surface\n");
2023-08-19 12:49:23 +00:00
return (-1);
}
oc_surface_swap_interval(surface, 0);
2023-08-19 12:49:23 +00:00
//TODO: create canvas
oc_canvas canvas = oc_canvas_create();
2023-08-19 12:49:23 +00:00
if(oc_canvas_is_nil(canvas))
2023-08-19 12:49:23 +00:00
{
oc_log_error("Error: couldn't create canvas\n");
2023-08-19 12:49:23 +00:00
return (-1);
}
oc_font font = create_font();
2023-08-19 12:49:23 +00:00
// start app
oc_window_bring_to_front(window);
oc_window_focus(window);
2023-08-19 12:49:23 +00:00
bool tracked = false;
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;
oc_input_state inputState = { 0 };
2023-08-19 12:49:23 +00:00
while(!oc_should_quit())
2023-08-19 12:49:23 +00:00
{
oc_arena_scope scratch = oc_scratch_begin();
f64 startTime = oc_clock_time(OC_CLOCK_MONOTONIC);
2023-08-19 12:49:23 +00:00
oc_pump_events(0);
oc_event* event = 0;
while((event = oc_next_event(scratch.arena)) != 0)
2023-08-19 12:49:23 +00:00
{
oc_input_process_event(&inputState, event);
2023-08-19 12:49:23 +00:00
switch(event->type)
{
case OC_EVENT_WINDOW_CLOSE:
2023-08-19 12:49:23 +00:00
{
oc_request_quit();
2023-08-19 12:49:23 +00:00
}
break;
case OC_EVENT_MOUSE_BUTTON:
2023-08-19 12:49:23 +00:00
{
if(event->key.code == OC_MOUSE_LEFT)
2023-08-19 12:49:23 +00:00
{
if(event->key.action == OC_KEY_PRESS)
2023-08-19 12:49:23 +00:00
{
tracked = true;
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;
case OC_EVENT_MOUSE_WHEEL:
2023-08-19 12:49:23 +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;
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;
case OC_EVENT_KEYBOARD_KEY:
2023-08-19 12:49:23 +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)
{
case OC_KEY_SPACE:
2023-08-19 12:49:23 +00:00
singlePath = !singlePath;
break;
case OC_KEY_UP:
2023-08-19 12:49:23 +00:00
{
if(event->key.mods & OC_KEYMOD_SHIFT)
2023-08-19 12:49:23 +00:00
{
singlePathIndex++;
}
else
{
zoom += 0.001;
}
}
break;
case OC_KEY_DOWN:
2023-08-19 12:49:23 +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)
{
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;
}
oc_surface_select(surface);
2023-08-19 12:49:23 +00:00
oc_set_color_rgba(1, 0, 1, 1);
oc_clear();
2023-08-19 12:49:23 +00:00
oc_matrix_multiply_push((oc_mat2x3){ zoom, 0, startX,
0, zoom, startY });
2023-08-19 12:49:23 +00:00
draw_tiger(singlePath, singlePathIndex);
if(singlePath)
{
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
}
oc_matrix_pop();
2023-08-19 12:49:23 +00:00
// text
oc_set_color_rgba(0, 0, 1, 1);
oc_set_font(font);
oc_set_font_size(12);
oc_move_to(50, 600 - 50);
oc_str8 text = oc_str8_pushf(scratch.arena,
"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);
oc_render(canvas);
oc_surface_present(surface);
oc_input_next_frame(&inputState);
oc_scratch_end(scratch);
frameTime = oc_clock_time(OC_CLOCK_MONOTONIC) - startTime;
2023-08-19 12:49:23 +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
oc_terminate();
2023-08-19 12:49:23 +00:00
return (0);
}