Tutorials

Tutorials for getting started with programming on Linux

Define Ball Struct

We want to have variables combined in a struct to make the code cleaner

Published
Liked the article? Share it!

So we don’t have a lot of random variables floating around, now’s a good time to combine related variables into a struct, so we can see which variables are related to which elements.

main.c
#include <epoxy/gl.h> #include <epoxy/glx.h> #include <gtk/gtk.h> #include <math.h> #include "DashGL/dashgl.h" #define WIDTH 640.0f #define HEIGHT 480.0f static void on_realize(GtkGLArea *area); static void on_render(GtkGLArea *area, GdkGLContext *context); static gboolean on_idle(gpointer data); struct { float dx, dy; vec3 pos; mat4 mvp; GLuint vbo_circle; } ball; GLuint program; GLuint vao; GLint attribute_coord2d; GLint uniform_mvp; int main(int argc, char *argv[]) { GtkWidget *window; GtkWidget *glArea; gtk_init(&argc, &argv); // Initialize Window window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(window), "Brickout Tutorial"); gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); gtk_window_set_default_size(GTK_WINDOW(window), 640, 480); gtk_window_set_type_hint(GTK_WINDOW(window), GDK_WINDOW_TYPE_HINT_UTILITY); g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL); // Initialize GTK GL Area glArea = gtk_gl_area_new(); gtk_widget_set_vexpand(glArea, TRUE); gtk_widget_set_hexpand(glArea, TRUE); g_signal_connect(glArea, "realize", G_CALLBACK(on_realize), NULL); g_signal_connect(glArea, "render", G_CALLBACK(on_render), NULL); gtk_container_add(GTK_CONTAINER(window), glArea); // Show widgets gtk_widget_show_all(window); gtk_main(); return 0; } static void on_realize(GtkGLArea *area) { // Debug Message g_print("on realize\n"); gtk_gl_area_make_current(area); if(gtk_gl_area_get_error(area) != NULL) { fprintf(stderr, "Unknown error\n"); return; } const GLubyte *renderer = glGetString(GL_RENDER); const GLubyte *version = glGetString(GL_VERSION); const GLubyte *shader = glGetString(GL_SHADING_LANGUAGE_VERSION); printf("Shader %s\n", shader); printf("Renderer: %s\n", renderer); printf("OpenGL version supported %s\n", version); glClearColor(1.0f, 1.0f, 1.0f, 1.0f); glGenVertexArrays(1, &vao); glBindVertexArray(vao); int i; float angle, nextAngle; int num_segments = 99; GLfloat circle_vertices[6 * 100]; for(i = 0; i &ball.vbo_circle); glBindBuffer(GL_ARRAY_BUFFER, ball.vbo_circle); glBufferData( GL_ARRAY_BUFFER, sizeof(circle_vertices), circle_vertices, GL_STATIC_DRAW ); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(0); glDisableVertexAttribArray(0); const char *vs = "shader/vertex.glsl"; const char *fs = "shader/fragment.glsl"; program = shader_load_program(vs, fs); const char *attribute_name = "coord2d"; attribute_coord2d = glGetAttribLocation(program, attribute_name); if(attribute_coord2d == -1) { fprintf(stderr, "Could not bind attribute %s\n", attribute_name); return; } const char *uniform_name = "orthograph"; GLint uniform_ortho = glGetUniformLocation(program, uniform_name); if(uniform_ortho == -1) { fprintf(stderr, "Could not bind uniform %s\n", uniform_name); return; } uniform_name = "mvp"; uniform_mvp = glGetUniformLocation(program, uniform_name); if(uniform_mvp == -1) { fprintf(stderr, "Could not bind uniform %s\n", uniform_name); return; } glUseProgram(program); mat4 orthograph; mat4_orthagonal(WIDTH, HEIGHT, orthograph); glUniformMatrix4fv(uniform_ortho, 1, GL_FALSE, orthograph); g_timeout_add(20, on_idle, (void*)area); ball.dx = 2.0f; ball.dy = 3.0f; ball.pos[0] = 50.0f; ball.pos[1] = 50.0f; ball.pos[2] = 0.0f; } static void on_render(GtkGLArea *area, GdkGLContext *context) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); mat4_translate(ball.pos, ball.mvp); glUniformMatrix4fv(uniform_mvp, 1, GL_FALSE, ball.mvp); glBindVertexArray(vao); glEnableVertexAttribArray(attribute_coord2d); glBindBuffer(GL_ARRAY_BUFFER, ball.vbo_circle); glVertexAttribPointer( attribute_coord2d, 2, GL_FLOAT, GL_FALSE, 0, 0 ); glDrawArrays(GL_TRIANGLES, 0, 3 * 100); glDisableVertexAttribArray(attribute_coord2d); } static gboolean on_idle(gpointer data) { ball.pos[0] += ball.dx; ball.pos[1] += ball.dy; if(ball.pos[0] > WIDTH) { ball.pos[0] = WIDTH; ball.dx *= -1; } else if(ball.pos[0] <0) { ball.pos[0] = 0; ball.dx *= -1; } if(ball.pos[1] > HEIGHT) { ball.pos[1] = HEIGHT; ball.dy *= -1; } else if(ball.pos[1] <0) { ball.pos[1] = 0; ball.dy *= -1; } gtk_widget_queue_draw(GTK_WIDGET(data)); return TRUE; }

Compile

$ gcc -c -o DashGL/dashgl.o DashGL/dashgl.c -lepoxy -lpng -lm
$ gcc `pkg-config --cflags gtk+-3.0` main.c DashGL/dashgl.o `pkg-config --libs gtk+-3.0` \
-lepoxy -lm -lpng

Run

$ ./a.out