summaryrefslogtreecommitdiff
path: root/graph.c
diff options
context:
space:
mode:
Diffstat (limited to 'graph.c')
-rw-r--r--graph.c81
1 files changed, 44 insertions, 37 deletions
diff --git a/graph.c b/graph.c
index fba4106..89ce317 100644
--- a/graph.c
+++ b/graph.c
@@ -6,23 +6,26 @@
#include"graph.h"
_ SDL_Window*win;
-_ U32 win_w=1280;
-_ U32 win_h=720;
_ SDL_GLContext ctx;
-
+_ U32 win_w=1280,win_h=720;
+_ IM C solid_vert[]="#version 330 core\nlayout(location=0)in vec3 pos;void main(){gl_Position=vec4(pos.x,pos.y,pos.z,1.0);}";
+_ IM C solid_frag[]="#version 330 core\nout vec4 Colour;void main(){Colour=vec4(1.0, 0.5, 0.2, 1.0);}";
+_ U32 solid_shader;
_ V on_err(IM C*d){die("Could not %s: %s\n",d,SDL_GetError());}
+_ V q_init(V);
V graph_init(V){SDL_version sdl_vh,sdl_vl;
Q(SDL_Init(SDL_INIT_VIDEO),on_err("init sdl2"))
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION,3),
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION,3),
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,SDL_GL_CONTEXT_PROFILE_CORE),
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
- N(win=SDL_CreateWindow("mino",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,win_w,win_h,SDL_WINDOW_OPENGL),on_err("create window"))
+ N(win=SDL_CreateWindow("mino",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,win_w,win_h,SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE),on_err("create window"))
N(ctx=SDL_GL_CreateContext(win),on_err("create OpenGL context"))
N(gladLoadGLLoader(SDL_GL_GetProcAddress),on_err("load GL"))
SDL_GL_SetSwapInterval(1);
-
+ q_init(),
+ solid_shader=graph_shader_create(solid_vert,solid_frag);
SDL_VERSION(&sdl_vh)SDL_GetVersion(&sdl_vl);
fprintf(stderr,
"Graphics initialization successful!\n"
@@ -35,42 +38,46 @@ V graph_init(V){SDL_version sdl_vh,sdl_vl;
sdl_vl.major,sdl_vl.minor,sdl_vl.patch,
glGetString(GL_VERSION),glGetString(GL_VENDOR),glGetString(GL_RENDERER));}
+V graph_events(V){SDL_Event ev;U32 t;WH(SDL_PollEvent(&ev),t=ev.type;
+ Q(SDL_QUIT==t,exit(0))OR
+ Q(SDL_WINDOWEVENT==t,SDL_WindowEvent ew=ev.window;
+ Q(SDL_WINDOWEVENT_RESIZED==ew.event
+ ||SDL_WINDOWEVENT_SIZE_CHANGED==ew.event,glViewport(0,0,ew.data1,ew.data2))))}
+
V graph_deinit(V){SDL_GL_DeleteContext(ctx),SDL_DestroyWindow(win),SDL_Quit();}
-V graph_events(V){SDL_Event ev;WH(SDL_PollEvent(&ev),Q(SDL_QUIT==ev.type,exit(0)))}
V graph_before(V){glClearColor(1.0,0.75,0.8,1.0),glClear(GL_COLOR_BUFFER_BIT);}
V graph_after(V){SDL_GL_SwapWindow(win);}
-V graph_triangle(V){
- IM C*vert_shader_src="#version 330 core\n"
- "layout(location=0)in vec3 pos;void main(){gl_Position=vec4(pos.x,pos.y,pos.z,1.0);}";
- U32 vert_shader=glCreateShader(GL_VERTEX_SHADER);
- glShaderSource(vert_shader,1,&vert_shader_src,NULL),glCompileShader(vert_shader);
- I success;
- C info_log[512];
- glGetShaderiv(vert_shader,GL_COMPILE_STATUS,&success);
- N(success,die("error compiling vert shader"))
-
- IM C*frag_shader_src = "#version 330 core\n"
- "out vec4 Colour;void main(){Colour=vec4(1.0, 0.5, 0.2, 1.0);}";
- U32 frag_shader=glCreateShader(GL_FRAGMENT_SHADER);
- glShaderSource(frag_shader,1,&frag_shader_src,NULL),glCompileShader(frag_shader);
- glGetShaderiv(vert_shader,GL_COMPILE_STATUS,&success);
- N(success,die("error compiling frag shader"))
-
- U32 shader_program=glCreateProgram();
- glAttachShader(shader_program,vert_shader),glAttachShader(shader_program,frag_shader),
- glLinkProgram(shader_program);//glDeleteShader(vert_shader),glDeleteShader(frag_shader);
- glGetProgramiv(shader_program,GL_LINK_STATUS,&success);
- N(success,glGetProgramInfoLog(shader_program,512,0,info_log),die("error linking: %s"))
-
- IM F32 verts[]={
- -0.5,-0.5,0.0,
+_ U32 q_vao,q_vbo,q_ebo;
+_ V q_init(V){
+ _ IM F32 verts[]={
+ 0.5, 0.5,0.0,
0.5,-0.5,0.0,
- 0.0, 0.5,0.0};
- U32 vbo,vao;
- glGenBuffers(1,&vbo), glBindBuffer(GL_ARRAY_BUFFER,vbo),
- glGenVertexArrays(1,&vao), glBindVertexArray(vao);
+ -0.5,-0.5,0.0,
+ -0.5, 0.5,0.0};
+ _ IM U32 indxs[]={0,1,3,1,2,3};
+ glGenVertexArrays(1,&q_vao),glGenBuffers(1,&q_vbo),glGenBuffers(1,&q_ebo);
+ glBindVertexArray(q_vao),
+ glBindBuffer(GL_ARRAY_BUFFER,q_vbo),
glBufferData(GL_ARRAY_BUFFER,SZ verts,verts,GL_STATIC_DRAW),
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,q_ebo),
+ glBufferData(GL_ELEMENT_ARRAY_BUFFER,SZ indxs,indxs,GL_STATIC_DRAW);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,3*SZ verts[0],0),
- glEnableVertexAttribArray(0),glUseProgram(shader_program),
- glBindVertexArray(vao),glDrawArrays(GL_TRIANGLES,0,3);}
+ glEnableVertexAttribArray(0),
+ glBindVertexArray(0);}
+V graph_quad(V){
+ glUseProgram(solid_shader),
+ glBindVertexArray(q_vao),
+ glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0),
+ glBindVertexArray(0);}
+
+U32 graph_shader_create(IM C*vert,IM C*frag){I ok;C info[512];U32 v,f,p;
+ v=glCreateShader(GL_VERTEX_SHADER), glShaderSource(v,1,&vert,0),glCompileShader(v),
+ glGetShaderiv(v,GL_COMPILE_STATUS,&ok);N(ok,glGetShaderInfoLog(v,512,0,info),die("error compiling vert: %s",info))
+ f=glCreateShader(GL_FRAGMENT_SHADER),glShaderSource(f,1,&frag,0),glCompileShader(f),
+ glGetShaderiv(f,GL_COMPILE_STATUS,&ok);N(ok,glGetShaderInfoLog(f,512,0,info),die("error compiling frag: %s",info))
+ p=glCreateProgram(),glAttachShader(p,v),glAttachShader(p,f),glLinkProgram(p);
+ glGetProgramiv(p,GL_LINK_STATUS,&ok); N(ok,glGetProgramInfoLog(p,512,0,info),die("error linking shader: %s",info))
+ glDeleteShader(v),glDeleteShader(f);
+ R p;}
+