2023-03-28 11:09:48 +00:00
|
|
|
/************************************************************//**
|
|
|
|
*
|
|
|
|
* @file: main.cpp
|
|
|
|
* @author: Martin Fouilleul
|
|
|
|
* @date: 30/07/2022
|
|
|
|
* @revision:
|
|
|
|
*
|
|
|
|
*****************************************************************/
|
2023-06-29 13:48:52 +00:00
|
|
|
#include<stdio.h>
|
2023-03-28 11:09:48 +00:00
|
|
|
#include<stdlib.h>
|
|
|
|
#include<string.h>
|
|
|
|
#include<errno.h>
|
|
|
|
|
|
|
|
#define _USE_MATH_DEFINES //NOTE: necessary for MSVC
|
|
|
|
#include<math.h>
|
|
|
|
|
|
|
|
#include"milepost.h"
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
mp_init();
|
|
|
|
mp_clock_init(); //TODO put that in mp_init()?
|
|
|
|
|
|
|
|
mp_rect windowRect = {.x = 100, .y = 100, .w = 810, .h = 610};
|
|
|
|
mp_window window = mp_window_create(windowRect, "test", 0);
|
|
|
|
|
|
|
|
mp_rect contentRect = mp_window_get_content_rect(window);
|
|
|
|
|
|
|
|
//NOTE: create surface
|
2023-06-29 13:48:52 +00:00
|
|
|
mg_surface surface = mg_surface_create_for_window(window, MG_CANVAS);
|
2023-06-30 13:18:37 +00:00
|
|
|
mg_surface_swap_interval(surface, 1);
|
2023-03-28 11:09:48 +00:00
|
|
|
|
2023-06-29 13:48:52 +00:00
|
|
|
if(mg_surface_is_nil(surface))
|
|
|
|
{
|
|
|
|
printf("Error: couldn't create surface\n");
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
2023-03-28 11:09:48 +00:00
|
|
|
//TODO: create canvas
|
2023-06-29 13:48:52 +00:00
|
|
|
mg_canvas canvas = mg_canvas_create();
|
2023-03-28 11:09:48 +00:00
|
|
|
|
|
|
|
if(mg_canvas_is_nil(canvas))
|
|
|
|
{
|
|
|
|
printf("Error: couldn't create canvas\n");
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// start app
|
|
|
|
mp_window_bring_to_front(window);
|
|
|
|
mp_window_focus(window);
|
|
|
|
|
|
|
|
f64 frameTime = 0;
|
2023-04-04 17:02:53 +00:00
|
|
|
f32 x = 0, y = 0;
|
2023-03-28 11:09:48 +00:00
|
|
|
|
|
|
|
while(!mp_should_quit())
|
|
|
|
{
|
|
|
|
f64 startTime = mp_get_time(MP_CLOCK_MONOTONIC);
|
|
|
|
|
|
|
|
mp_pump_events(0);
|
2023-06-29 13:48:52 +00:00
|
|
|
mp_event* event = 0;
|
|
|
|
while((event = mp_next_event(mem_scratch())) != 0)
|
2023-03-28 11:09:48 +00:00
|
|
|
{
|
2023-06-29 13:48:52 +00:00
|
|
|
switch(event->type)
|
2023-03-28 11:09:48 +00:00
|
|
|
{
|
|
|
|
case MP_EVENT_WINDOW_CLOSE:
|
|
|
|
{
|
|
|
|
mp_request_quit();
|
|
|
|
} break;
|
|
|
|
|
2023-04-04 17:02:53 +00:00
|
|
|
case MP_EVENT_KEYBOARD_KEY:
|
|
|
|
{
|
2023-06-29 13:48:52 +00:00
|
|
|
if(event->key.action == MP_KEY_PRESS)
|
2023-04-04 17:02:53 +00:00
|
|
|
{
|
2023-06-29 13:48:52 +00:00
|
|
|
if(event->key.code == MP_KEY_LEFT)
|
2023-04-04 17:02:53 +00:00
|
|
|
{
|
|
|
|
x-=1;
|
|
|
|
}
|
2023-06-29 13:48:52 +00:00
|
|
|
if(event->key.code == MP_KEY_RIGHT)
|
2023-04-04 17:02:53 +00:00
|
|
|
{
|
|
|
|
x+=1;
|
|
|
|
}
|
2023-06-29 13:48:52 +00:00
|
|
|
if(event->key.code == MP_KEY_UP)
|
2023-04-04 17:02:53 +00:00
|
|
|
{
|
|
|
|
y-=1;
|
|
|
|
}
|
2023-06-29 13:48:52 +00:00
|
|
|
if(event->key.code == MP_KEY_DOWN)
|
2023-04-04 17:02:53 +00:00
|
|
|
{
|
|
|
|
y+=1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
2023-03-28 11:09:48 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mg_surface_prepare(surface);
|
|
|
|
|
|
|
|
// background
|
|
|
|
mg_set_color_rgba(0, 1, 1, 1);
|
|
|
|
mg_clear();
|
2023-06-29 13:48:52 +00:00
|
|
|
|
2023-03-28 11:09:48 +00:00
|
|
|
mg_move_to(100, 100);
|
|
|
|
mg_line_to(150, 150);
|
|
|
|
mg_line_to(100, 200);
|
|
|
|
mg_line_to(50, 150);
|
|
|
|
mg_close_path();
|
|
|
|
mg_set_color_rgba(1, 0, 0, 1);
|
|
|
|
mg_fill();
|
|
|
|
|
|
|
|
mg_move_to(200, 100);
|
2023-03-28 15:58:23 +00:00
|
|
|
mg_line_to(410, 100);
|
|
|
|
mg_line_to(410, 200);
|
2023-03-28 11:09:48 +00:00
|
|
|
mg_line_to(200, 200);
|
|
|
|
mg_close_path();
|
|
|
|
mg_set_color_rgba(0, 1, 0, 1);
|
|
|
|
mg_fill();
|
2023-07-03 09:02:04 +00:00
|
|
|
|
2023-07-03 10:14:21 +00:00
|
|
|
mg_set_color_rgba(0, 1, 1, 0.5);
|
2023-07-03 09:02:04 +00:00
|
|
|
mg_rectangle_fill(120, 120, 200, 200);
|
|
|
|
|
2023-07-03 10:14:21 +00:00
|
|
|
mg_set_color_rgba(1, 0, 0.5, 1);
|
|
|
|
mg_rectangle_fill(700, 500, 200, 200);
|
|
|
|
|
|
|
|
mg_move_to(300, 300);
|
|
|
|
mg_quadratic_to(400, 500, 500, 300);
|
2023-03-30 13:58:46 +00:00
|
|
|
mg_close_path();
|
|
|
|
mg_set_color_rgba(0, 0, 1, 1);
|
|
|
|
mg_fill();
|
2023-07-03 12:21:53 +00:00
|
|
|
|
|
|
|
mg_move_to(200, 450);
|
|
|
|
mg_cubic_to(200, 250, 400, 550, 400, 450);
|
2023-04-01 17:46:35 +00:00
|
|
|
mg_close_path();
|
2023-07-03 12:21:53 +00:00
|
|
|
mg_set_color_rgba(1, 0.5, 0, 1);
|
2023-04-01 17:46:35 +00:00
|
|
|
mg_fill();
|
2023-07-03 12:21:53 +00:00
|
|
|
|
2023-04-11 09:16:23 +00:00
|
|
|
/*
|
2023-04-05 08:25:48 +00:00
|
|
|
mg_set_joint(MG_JOINT_NONE);
|
2023-04-04 18:35:48 +00:00
|
|
|
mg_set_max_joint_excursion(20);
|
|
|
|
|
2023-04-05 08:25:48 +00:00
|
|
|
mg_set_cap(MG_CAP_SQUARE);
|
2023-04-04 18:35:48 +00:00
|
|
|
|
2023-04-05 08:19:25 +00:00
|
|
|
mg_move_to(x+200, y+200);
|
|
|
|
mg_line_to(x+300, y+300);
|
|
|
|
mg_line_to(x+200, y+400);
|
|
|
|
mg_line_to(x+100, y+300);
|
2023-04-04 17:02:53 +00:00
|
|
|
mg_close_path();
|
|
|
|
mg_set_color_rgba(1, 0, 0, 1);
|
|
|
|
// mg_set_width(2);
|
|
|
|
mg_stroke();
|
|
|
|
|
|
|
|
mg_move_to(400, 400);
|
|
|
|
mg_quadratic_to(600, 601, 800, 400);
|
|
|
|
mg_set_color_rgba(0, 0, 1, 1);
|
|
|
|
mg_stroke();
|
|
|
|
|
2023-04-05 08:31:50 +00:00
|
|
|
mg_move_to(x+400, y+300);
|
|
|
|
mg_cubic_to(x+400, y+100, x+600, y+400, x+600, y+300);
|
2023-04-04 17:02:53 +00:00
|
|
|
mg_close_path();
|
|
|
|
mg_set_color_rgba(0, 0, 1, 1);
|
|
|
|
mg_stroke();
|
2023-06-29 13:48:52 +00:00
|
|
|
|
2023-04-11 09:16:23 +00:00
|
|
|
mg_set_color_rgba(1, 0, 0, 1);
|
|
|
|
mg_rounded_rectangle_fill(100, 100, 200, 300, 20);
|
2023-04-04 17:02:53 +00:00
|
|
|
|
|
|
|
mg_move_to(x+8, y+8);
|
|
|
|
mg_line_to(x+33, y+8);
|
|
|
|
mg_line_to(x+33, y+19);
|
|
|
|
mg_line_to(x+8, y+19);
|
|
|
|
mg_close_path();
|
|
|
|
mg_set_color_rgba(0, 0, 1, 1);
|
|
|
|
mg_fill();
|
|
|
|
*/
|
2023-03-28 11:09:48 +00:00
|
|
|
printf("Milepost vector graphics test program (frame time = %fs, fps = %f)...\n",
|
|
|
|
frameTime,
|
|
|
|
1./frameTime);
|
|
|
|
|
2023-06-29 13:48:52 +00:00
|
|
|
mg_render(surface, canvas);
|
2023-03-28 11:09:48 +00:00
|
|
|
mg_surface_present(surface);
|
|
|
|
|
|
|
|
mem_arena_clear(mem_scratch());
|
|
|
|
frameTime = mp_get_time(MP_CLOCK_MONOTONIC) - startTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
mg_canvas_destroy(canvas);
|
|
|
|
mg_surface_destroy(surface);
|
|
|
|
mp_window_destroy(window);
|
|
|
|
|
|
|
|
mp_terminate();
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|