#include #include #define SDL_MAIN_HANDLED #include #include #include #include #include"mino.h" #include"graph.h" #include"tex.h" U32 graph_win_w=1280,graph_win_h=720; _ SDL_Window*win; _ SDL_GLContext ctx; _ V on_err(IM C*d){die("Could not %s: %s",d,SDL_GetError());} _ V q_init(V);_ V q_deinit(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,graph_win_w,graph_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), glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA), glEnable(GL_BLEND), q_init(); SDL_VERSION(&sdl_vh)SDL_GetVersion(&sdl_vl); fprintf(stderr, "Graphics initialization successful!\n" "\tSDL Version: %d.%d.%d (built against)\n" "\tSDL Version: %d.%d.%d (linked against)\n" "\tOpenGL Version: %s\n" "\tOpenGL Vendor: %s\n" "\tOpenGL Renderer: %s\n", sdl_vh.major,sdl_vh.minor,sdl_vh.patch, 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,graph_win_w=ew.data1,graph_win_h=ew.data2, glViewport(0,0,ew.data1,ew.data2))))} V graph_deinit(V){q_deinit(),SDL_GL_DeleteContext(ctx),SDL_DestroyWindow(win),SDL_Quit();} 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);} U32 graph_ticks(V){R SDL_GetTicks();} _ U32 q_vao,q_vbo,q_ebo; _ V q_init(V){ _ IM F32 verts[]={1,1,0, 1,1, 1,0,0, 1,0, 0,0,0, 0,0, 0,1,0, 0,1}; _ 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,5*SZ(F32),(V*)(0*SZ(F32))), glEnableVertexAttribArray(0), glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,5*SZ(F32),(V*)(3*SZ(F32))), glEnableVertexAttribArray(1), glBindVertexArray(0);} _ V q_deinit(V){glDeleteBuffers(1,&q_vao),glDeleteBuffers(1,&q_vbo),glDeleteBuffers(1,&q_ebo);} V graph_quad_xywh(F32 x,F32 y,F32 w,F32 h){graph_quad((vec2){x,y},(vec2){w,h});} V graph_quad(vec2 p,vec2 z){mat4 trans; glm_mat4_identity(trans), glm_translate(trans,(vec3){p[0],p[1],0}), glm_scale(trans,(vec3){z[0],z[1],1}), graph_shader_setM4("model",trans), 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;} V graph_shader_use(U32 s){glUseProgram(s);} V graph_shader_setI(IM C*s,I i) {I p;glGetIntegerv(GL_CURRENT_PROGRAM,&p),glUniform1i(glGetUniformLocation(p,s),i);} V graph_shader_setV3(IM C*s,vec3 v3){I p;glGetIntegerv(GL_CURRENT_PROGRAM,&p),glUniform3fv(glGetUniformLocation(p,s),1,v3);} V graph_shader_setV4(IM C*s,vec4 v4){I p;glGetIntegerv(GL_CURRENT_PROGRAM,&p),glUniform4fv(glGetUniformLocation(p,s),1,v4);} V graph_shader_setM4(IM C*s,mat4 m4){I p;glGetIntegerv(GL_CURRENT_PROGRAM,&p),glUniformMatrix4fv(glGetUniformLocation(p,s),1,GL_FALSE,&m4[0][0]);} U32 graph_tex_create(IM C*f){Tex tex;U32 t; glGenTextures(1,&t); glBindTexture(GL_TEXTURE_2D,t), glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST_MIPMAP_NEAREST), glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); tex=tex_load(f); glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,tex.w,tex.h,0,GL_RGBA,GL_UNSIGNED_BYTE,tex.d), glGenerateMipmap(GL_TEXTURE_2D), tex_free(tex); R t;} V graph_tex_use(U32 tex,U8 i){ glActiveTexture(GL_TEXTURE0+i); glBindTexture(GL_TEXTURE_2D,tex);}