1212
1313Drawer::Drawer (Game& g)
1414{
15-
1615 // standard rect EBO
1716 {
1817 unsigned int indices[] = {
@@ -28,8 +27,11 @@ Drawer::Drawer(Game& g)
2827 glBufferData (GL_ARRAY_BUFFER, sizeof (indices), indices, GL_STATIC_DRAW);
2928 }
3029
31- // font stuff with freetype
30+ // load all programs
31+ this ->load_all_programs (g.l );
32+
3233
34+ // font stuff with freetype
3335 {
3436 FT_Library ft;
3537 if (FT_Init_FreeType (&ft)) {
@@ -76,20 +78,17 @@ Drawer::Drawer(Game& g)
7678
7779 unsigned int width = 0 , height = 0 ;
7880
79- static constexpr int PIX_OFFSET = 10 ;
81+ // the offset between characters in the texture
82+ static constexpr int PIX_OFFSET = 1 ;
8083
8184 for (unsigned char c = CHARACTERS_START_AT; c < CHARACTERS; c++)
8285 {
83- // load character glyph
86+ // load character glyph
8487 if (FT_Load_Char (face, c, FT_LOAD_RENDER))
8588 {
8689 std::cout << " ERROR::FREETYTPE: Failed to load Glyph:" << c << std::endl;
8790 continue ;
8891 }
89- // generate texture
90- // glTexImage2D(GL_TEXTURE_2D,0, GL_RED, face->glyph->bitmap.width,
91- // face->glyph->bitmap.rows, 0, GL_RED, GL_UNSIGNED_BYTE, face->glyph->bitmap.buffer
92- // );
9392 width += face->glyph ->bitmap .width + PIX_OFFSET;
9493 height = std::max (height, face->glyph ->bitmap .rows );
9594 // now store character for later use
@@ -108,8 +107,6 @@ Drawer::Drawer(Game& g)
108107 glGenTextures (1 , &m_main_font_tex);
109108 glBindTexture (GL_TEXTURE_2D, m_main_font_tex); // all upcoming GL_TEXTURE_2D operations now have effect on this texture object
110109
111- glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
112-
113110 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
114111 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
115112 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
@@ -168,13 +165,13 @@ Drawer::Drawer(Game& g)
168165 // make it the correct amount of data
169166 float data[CHARACTERS][U_TEXT_ATTRIBUTES_PER]; // store the width,height,bearingx,bearingy
170167 for (int i = CHARACTERS_START_AT; i < CHARACTERS; ++i) {
171- data[i][0 ] = m_characters[i].width ; // width
172- data[i][1 ] = m_characters[i].height ; // height
173- data[i][2 ] = m_characters[i].bearing_x ; // bearing X
174- data[i][3 ] = m_characters[i].bearing_y ; // bearing Y
168+ data[i][0 ] = ( float ) m_characters[i].width ; // width
169+ data[i][1 ] = ( float ) m_characters[i].height ; // height
170+ data[i][2 ] = ( float ) m_characters[i].bearing_x ; // bearing X
171+ data[i][3 ] = ( float ) m_characters[i].bearing_y ; // bearing Y
175172 }
176173
177- glUniform4fv (glGetUniformLocation (m_text_program, " u_text" ), 128 , (float *)data);
174+ glUniform4fv (glGetUniformLocation (m_text_program, " u_text" ), CHARACTERS , (float *)data);
178175 }
179176
180177 {
@@ -185,14 +182,16 @@ Drawer::Drawer(Game& g)
185182 x += m_characters[i].width + PIX_OFFSET;
186183 }
187184
188- glUniform1fv (glGetUniformLocation (m_text_program, " u_char_x_offsets" ), 128 , (float *)char_x_offset_data);
185+ glUniform1fv (glGetUniformLocation (m_text_program, " u_char_x_offsets" ), CHARACTERS, (float *)char_x_offset_data);
186+ }
187+
188+ {
189+ m_text_u_color = glGetUniformLocation (m_text_program, " u_color" );
189190 }
190191 }
191192
192193 // rectangle shader
193194 {
194- rectangle_program = g.l .compile_shader_program (" f/shaders/rectangle.vert" , " f/shaders/rectangle.frag" , " rectangle shader" );
195-
196195 float vertices[] = {
197196 -0 .5f , -0 .5f ,
198197 -0 .5f , 0 .5f ,
@@ -219,7 +218,7 @@ Drawer::Drawer(Game& g)
219218
220219 // image shader
221220 {
222- image_program = g. l . compile_shader_program ( " f/shaders/image.vert " , " f/shaders/image.frag " , " image shader " );
221+
223222 float vertices[] = { // changed later
224223 // position texture coordinates
225224 -0 .5f , -0 .5f , 0 .f , 0 .f ,
@@ -248,8 +247,6 @@ Drawer::Drawer(Game& g)
248247
249248 // sky shader
250249 {
251- sky_program = g.l .compile_shader_program (" f/shaders/sky.vert" , " f/shaders/sky.frag" , " sky shader" );
252-
253250 const float y_imgs = 4 .f * g.l .HEIGHT ;
254251 const float x_imgs = 4 .f * g.l .WIDTH ;
255252
@@ -284,8 +281,7 @@ Drawer::Drawer(Game& g)
284281 }
285282
286283 // sides shader
287- {
288- sides_program = g.l .compile_shader_program (" f/shaders/sides.vert" , " f/shaders/sides.frag" , " sides shader" );
284+ {
289285 glGenVertexArrays (1 , &sides_VAO);
290286 glBindVertexArray (sides_VAO);
291287
@@ -329,8 +325,7 @@ Drawer::Drawer(Game& g)
329325 }
330326
331327 // cloud program
332- {
333- cloud_program = g.l .compile_shader_program (" f/shaders/cloud.vert" , " f/shaders/cloud.frag" , " cloud shader" );
328+ {
334329 glGenVertexArrays (1 , &cloud_VAO);
335330 glBindVertexArray (cloud_VAO);
336331
@@ -354,7 +349,7 @@ Drawer::Drawer(Game& g)
354349
355350 // coin particle program
356351 {
357- coin_particle_program = g. l . compile_shader_program ( " f/shaders/coin_particle.vert " , " f/shaders/coin_particle.frag " , " coin particle shader " );
352+
358353 glGenVertexArrays (1 , &coin_particle_VAO);
359354 glBindVertexArray (coin_particle_VAO);
360355
@@ -378,7 +373,7 @@ Drawer::Drawer(Game& g)
378373
379374 // bird program
380375 {
381- bird_program = g. l . compile_shader_program ( " f/shaders/bird.vert " , " f/shaders/bird.frag " , " bird shader " );
376+
382377 glGenVertexArrays (1 , &bird_VAO);
383378 glBindVertexArray (bird_VAO);
384379
@@ -429,7 +424,7 @@ Drawer::Drawer(Game& g)
429424 }
430425
431426 // mess with a texture2D
432- {
427+ /* {
433428 unsigned char data[100][100][4];
434429 for (int i = 0; i < 100; ++i) {
435430 for (int j = 0; j < 100; ++j) {
@@ -444,7 +439,7 @@ Drawer::Drawer(Game& g)
444439 glBindTexture(GL_TEXTURE_2D, texs[TEX::side_background]);
445440 glTexSubImage2D(GL_TEXTURE_2D, i, 100, 100, 100, 100, GL_RGBA, GL_UNSIGNED_BYTE, data);
446441 }
447- }
442+ }*/
448443}
449444
450445void Drawer::draw_text (const char * text, Color color, float x, float y, float scale)
@@ -474,6 +469,7 @@ void Drawer::draw_text(const char* text, Color color, float x, float y, float sc
474469 glBufferSubData (GL_ARRAY_BUFFER, 0 , nr_of_chars * TEXT_BYTES_PER, data);
475470
476471 glUniform3f (m_text_u_offset_scale, x, y, scale);
472+ glUniform4f (m_text_u_color, color.r , color.b , color.g , color.a );
477473
478474 glDrawElementsInstanced (GL_TRIANGLES, 6 , GL_UNSIGNED_INT, NULL , nr_of_chars);
479475}
@@ -667,6 +663,51 @@ void Drawer::before_draw(Game& g)
667663 float data[4 ] = { g.m_death_y , g.c .y , g.m_timer , g.l .WIDTH };
668664 glBufferSubData (GL_UNIFORM_BUFFER, 0 , UBO_GLOBAL_SIZE, &data);
669665 }
666+
667+ // hot reloading of shaders
668+ if constexpr (DEV_TOOLS) {
669+ if (g.l .key_just_down (SDL_SCANCODE_R)) {
670+ load_all_programs (g.l );
671+ }
672+ }
673+ }
674+
675+ void Drawer::draw_fps (float dt)
676+ {
677+ static constexpr int SIZE = 10 ;
678+ char fps_string[SIZE];
679+ snprintf (fps_string, SIZE, " %f" , 1 .f /dt);
680+
681+ draw_text (fps_string, Color{ 0 ,0 ,0 ,1 }, -Layer::WIDTH + 0 .1f , Layer::HEIGHT - 0 .1f , 0 .001f );
682+ }
683+
684+
685+ void Drawer::load_all_programs (Layer& l)
686+ {
687+ // the uniforms for the text shader are only set once, and they are removed when reloading shader...
688+ // glDeleteProgram(m_text_program);
689+ // m_text_program = l.compile_shader_program("f/shaders/text.vert", "f/shaders/text.frag", "text shader");
690+
691+ glDeleteProgram (rectangle_program);
692+ rectangle_program = l.compile_shader_program (" f/shaders/rectangle.vert" , " f/shaders/rectangle.frag" , " rectangle shader" );
693+
694+ glDeleteProgram (image_program);
695+ image_program = l.compile_shader_program (" f/shaders/image.vert" , " f/shaders/image.frag" , " image shader" );
696+
697+ glDeleteProgram (sky_program);
698+ sky_program = l.compile_shader_program (" f/shaders/sky.vert" , " f/shaders/sky.frag" , " sky shader" );
699+
700+ glDeleteProgram (sides_program);
701+ sides_program = l.compile_shader_program (" f/shaders/sides.vert" , " f/shaders/sides.frag" , " sides shader" );
702+
703+ glDeleteProgram (cloud_program);
704+ cloud_program = l.compile_shader_program (" f/shaders/cloud.vert" , " f/shaders/cloud.frag" , " cloud shader" );
705+
706+ glDeleteProgram (coin_particle_program);
707+ coin_particle_program = l.compile_shader_program (" f/shaders/coin_particle.vert" , " f/shaders/coin_particle.frag" , " coin particle shader" );
708+
709+ glDeleteProgram (bird_program);
710+ bird_program = l.compile_shader_program (" f/shaders/bird.vert" , " f/shaders/bird.frag" , " bird shader" );
670711}
671712
672713std::array<int , 2 > Drawer::load_texture (const char * path, unsigned int * image, int wrapping_x, int wrapping_y)
0 commit comments