2023-08-19 12:49:23 +00:00
|
|
|
/************************************************************/ /**
|
2023-04-11 08:50:14 +00:00
|
|
|
*
|
|
|
|
* @file: main.cpp
|
|
|
|
* @author: Martin Fouilleul
|
|
|
|
* @date: 30/07/2022
|
|
|
|
* @revision:
|
|
|
|
*
|
|
|
|
*****************************************************************/
|
2023-08-19 12:49:23 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2023-04-11 08:50:14 +00:00
|
|
|
|
|
|
|
#define _USE_MATH_DEFINES //NOTE: necessary for MSVC
|
2023-08-19 12:49:23 +00:00
|
|
|
#include <math.h>
|
2023-04-11 08:50:14 +00:00
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
#define OC_INCLUDE_GL_API
|
|
|
|
#include "orca.h"
|
2023-04-11 08:50:14 +00:00
|
|
|
|
2023-07-25 13:55:09 +00:00
|
|
|
unsigned int program;
|
|
|
|
|
|
|
|
const char* vshaderSource =
|
2023-08-19 12:49:23 +00:00
|
|
|
"#version 430\n"
|
2023-08-24 22:30:20 +00:00
|
|
|
"in vec4 vPosition;\n"
|
2023-08-19 12:49:23 +00:00
|
|
|
"uniform mat4 transform;\n"
|
|
|
|
"void main()\n"
|
|
|
|
"{\n"
|
|
|
|
" gl_Position = transform*vPosition;\n"
|
|
|
|
"}\n";
|
2023-07-25 13:55:09 +00:00
|
|
|
|
|
|
|
const char* fshaderSource =
|
2023-08-19 12:49:23 +00:00
|
|
|
"#version 430\n"
|
2023-08-24 22:30:20 +00:00
|
|
|
"layout(location = 0) out vec4 diffuse;"
|
2023-08-19 12:49:23 +00:00
|
|
|
"precision mediump float;\n"
|
|
|
|
"void main()\n"
|
|
|
|
"{\n"
|
2023-08-24 22:30:20 +00:00
|
|
|
" diffuse = vec4(1.0, 0.0, 0.0, 1.0);\n"
|
2023-08-19 12:49:23 +00:00
|
|
|
"}\n";
|
2023-07-25 13:55:09 +00:00
|
|
|
|
|
|
|
void compile_shader(GLuint shader, const char* source)
|
2023-04-11 08:50:14 +00:00
|
|
|
{
|
2023-08-19 12:49:23 +00:00
|
|
|
glShaderSource(shader, 1, &source, 0);
|
|
|
|
glCompileShader(shader);
|
|
|
|
|
|
|
|
int err = glGetError();
|
|
|
|
if(err)
|
|
|
|
{
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_log_error("gl error: %i\n", err);
|
2023-08-19 12:49:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int status = 0;
|
|
|
|
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
|
|
|
|
if(!status)
|
|
|
|
{
|
|
|
|
char buffer[256];
|
|
|
|
int size = 0;
|
|
|
|
glGetShaderInfoLog(shader, 256, &size, buffer);
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_log_error("shader error: %.*s\n", size, buffer);
|
2023-08-19 12:49:23 +00:00
|
|
|
}
|
2023-04-11 08:50:14 +00:00
|
|
|
}
|
|
|
|
|
2023-07-25 13:55:09 +00:00
|
|
|
GLfloat vertices[] = {
|
2023-08-19 12:49:23 +00:00
|
|
|
-0.866 / 2, -0.5 / 2, 0, 0.866 / 2, -0.5 / 2, 0, 0, 0.5, 0
|
|
|
|
};
|
2023-07-25 13:55:09 +00:00
|
|
|
|
2023-04-11 08:50:14 +00:00
|
|
|
typedef struct app_data
|
|
|
|
{
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_window window;
|
|
|
|
oc_surface surface;
|
|
|
|
oc_canvas canvas;
|
|
|
|
oc_font font;
|
2023-04-11 08:50:14 +00:00
|
|
|
|
2023-08-19 12:49:23 +00:00
|
|
|
GLuint vertexBuffer;
|
2023-04-11 08:50:14 +00:00
|
|
|
} app_data;
|
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
void process_event(app_data* app, oc_event event)
|
2023-04-11 08:50:14 +00:00
|
|
|
{
|
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_WINDOW_RESIZE:
|
2023-08-19 12:49:23 +00:00
|
|
|
{
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_log_info("resizing window!\n");
|
2023-08-19 12:49:23 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2023-04-11 08:50:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void update_and_render(app_data* app)
|
|
|
|
{
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_surface_select(app->surface);
|
2023-04-11 08:50:14 +00:00
|
|
|
|
2023-08-19 12:49:23 +00:00
|
|
|
glClearColor(0.3, 0.3, 1, 1);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
2023-04-11 08:50:14 +00:00
|
|
|
|
2023-08-19 12:49:23 +00:00
|
|
|
static float alpha = 0;
|
|
|
|
//f32 aspect = frameSize.x/frameSize.y;
|
|
|
|
f32 aspect = 800 / (f32)600;
|
2023-04-11 08:50:14 +00:00
|
|
|
|
2023-08-19 12:49:23 +00:00
|
|
|
GLfloat matrix[] = { cosf(alpha) / aspect, sinf(alpha), 0, 0,
|
|
|
|
-sinf(alpha) / aspect, cosf(alpha), 0, 0,
|
|
|
|
0, 0, 1, 0,
|
|
|
|
0, 0, 0, 1 };
|
2023-04-11 08:50:14 +00:00
|
|
|
|
2023-08-19 12:49:23 +00:00
|
|
|
alpha += 2 * M_PI / 120;
|
2023-04-11 08:50:14 +00:00
|
|
|
|
2023-08-19 12:49:23 +00:00
|
|
|
glUniformMatrix4fv(0, 1, false, matrix);
|
2023-04-11 08:50:14 +00:00
|
|
|
|
2023-08-19 12:49:23 +00:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, app->vertexBuffer);
|
|
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
|
|
|
glEnableVertexAttribArray(0);
|
2023-04-11 08:50:14 +00:00
|
|
|
|
2023-08-19 12:49:23 +00:00
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
2023-04-11 08:50:14 +00:00
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_surface_present(app->surface);
|
2023-04-11 08:50:14 +00:00
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_arena_clear(oc_scratch());
|
2023-04-11 08:50:14 +00:00
|
|
|
}
|
|
|
|
|
2023-07-31 04:21:25 +00:00
|
|
|
i32 render(void* user)
|
2023-04-11 08:50:14 +00:00
|
|
|
{
|
2023-08-19 12:49:23 +00:00
|
|
|
app_data* app = (app_data*)user;
|
2023-07-25 13:55:09 +00:00
|
|
|
|
2023-08-19 12:49:23 +00:00
|
|
|
//NOTE: init shader and gl state
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_surface_select(app->surface);
|
2023-07-25 13:55:09 +00:00
|
|
|
|
2023-08-19 12:49:23 +00:00
|
|
|
GLuint vao;
|
|
|
|
glGenVertexArrays(1, &vao);
|
|
|
|
glBindVertexArray(vao);
|
2023-07-25 13:55:09 +00:00
|
|
|
|
2023-08-19 12:49:23 +00:00
|
|
|
glGenBuffers(1, &app->vertexBuffer);
|
2023-07-25 13:55:09 +00:00
|
|
|
|
2023-08-19 12:49:23 +00:00
|
|
|
GLfloat vertices[] = {
|
|
|
|
-0.866 / 2, -0.5 / 2, 0, 0.866 / 2, -0.5 / 2, 0, 0, 0.5, 0
|
|
|
|
};
|
2023-07-25 13:55:09 +00:00
|
|
|
|
2023-08-19 12:49:23 +00:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, app->vertexBuffer);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
2023-07-25 13:55:09 +00:00
|
|
|
|
2023-08-19 12:49:23 +00:00
|
|
|
unsigned int vshader = glCreateShader(GL_VERTEX_SHADER);
|
|
|
|
unsigned int fshader = glCreateShader(GL_FRAGMENT_SHADER);
|
|
|
|
program = glCreateProgram();
|
2023-07-25 13:55:09 +00:00
|
|
|
|
2023-08-19 12:49:23 +00:00
|
|
|
compile_shader(vshader, vshaderSource);
|
|
|
|
compile_shader(fshader, fshaderSource);
|
2023-07-25 13:55:09 +00:00
|
|
|
|
2023-08-19 12:49:23 +00:00
|
|
|
glAttachShader(program, vshader);
|
|
|
|
glAttachShader(program, fshader);
|
|
|
|
glLinkProgram(program);
|
2023-07-25 13:55:09 +00:00
|
|
|
|
2023-08-19 12:49:23 +00:00
|
|
|
int status = 0;
|
|
|
|
glGetProgramiv(program, GL_LINK_STATUS, &status);
|
|
|
|
if(!status)
|
|
|
|
{
|
|
|
|
char buffer[256];
|
|
|
|
int size = 0;
|
|
|
|
glGetProgramInfoLog(program, 256, &size, buffer);
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_log_error("link error: %.*s\n", size, buffer);
|
2023-08-19 12:49:23 +00:00
|
|
|
}
|
2023-07-25 13:55:09 +00:00
|
|
|
|
2023-08-19 12:49:23 +00:00
|
|
|
glUseProgram(program);
|
2023-04-11 08:50:14 +00:00
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
while(!oc_should_quit())
|
2023-08-19 12:49:23 +00:00
|
|
|
{
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_event* event = 0;
|
2023-07-25 13:55:09 +00:00
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
while((event = oc_next_event(oc_scratch())) != 0)
|
2023-08-19 12:49:23 +00:00
|
|
|
{
|
|
|
|
process_event(app, *event);
|
|
|
|
}
|
|
|
|
update_and_render(app);
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_arena_clear(oc_scratch());
|
2023-08-19 12:49:23 +00:00
|
|
|
}
|
2023-04-11 08:50:14 +00:00
|
|
|
|
2023-08-19 12:49:23 +00:00
|
|
|
return (0);
|
2023-04-11 08:50:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_init();
|
2023-04-11 08:50:14 +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-04-11 08:50:14 +00:00
|
|
|
|
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_GL);
|
|
|
|
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("Error: couldn't create surface\n");
|
2023-08-19 12:49:23 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
2023-07-25 13:55:09 +00:00
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_surface_swap_interval(surface, 1);
|
|
|
|
oc_surface_deselect();
|
2023-04-11 08:50:14 +00:00
|
|
|
|
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-04-11 08:50:14 +00:00
|
|
|
|
2023-08-19 12:49:23 +00:00
|
|
|
//TODO: start thread
|
|
|
|
app_data app = { .window = window,
|
|
|
|
.surface = surface };
|
2023-04-11 08:50:14 +00:00
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_thread* renderThread = oc_thread_create(render, &app);
|
2023-04-11 08:50:14 +00:00
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
while(!oc_should_quit())
|
2023-08-19 12:49:23 +00:00
|
|
|
{
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_pump_events(0);
|
2023-08-19 12:49:23 +00:00
|
|
|
}
|
2023-04-11 08:50:14 +00:00
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_thread_join(renderThread, NULL);
|
2023-04-11 08:50:14 +00:00
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_surface_destroy(surface);
|
|
|
|
oc_window_destroy(window);
|
2023-04-11 08:50:14 +00:00
|
|
|
|
2023-08-24 22:30:20 +00:00
|
|
|
oc_terminate();
|
2023-04-11 08:50:14 +00:00
|
|
|
|
2023-08-19 12:49:23 +00:00
|
|
|
return (0);
|
2023-04-11 08:50:14 +00:00
|
|
|
}
|