Skip to content

Instantly share code, notes, and snippets.

@tesfabpel

tesfabpel/hang.c Secret

Created Nov 20, 2016
Embed
What would you like to do?
#include <math.h>
#include <GL/glew.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <SDL2/SDL.h>
#include "utils.h"
int main(int argc, char *argv[])
{
printf("Hello, World!\n");
//glutInit(&argc, argv);
//glewInit();
if(SDL_Init(SDL_INIT_VIDEO) < 0)
goto exit;
SDL_Window *window;
SDL_Renderer *renderer;
window = SDL_CreateWindow("Hello, World",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
640,
480,
SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL);
// OpenGL 4.x Core profile
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
renderer = SDL_GL_CreateContext(window);
//GLuint id;
//glGenBuffers(1, &id);
float hue = 0.0f;
float step = 1.0f / 10.0f;
Uint64 time_now = SDL_GetPerformanceCounter();
Uint64 time_last;
float delta_time;
while(1)
{
SDL_Event event;
SDL_PollEvent(&event);
if(event.type == SDL_QUIT)
{
break;
}
// Calculate delta time
time_last = time_now;
time_now = SDL_GetPerformanceCounter();
delta_time = (float) ((time_now - time_last) * 1000 / SDL_GetPerformanceFrequency());
// Calculate bg color
hue += step * (delta_time / 1000.0f);
hue = fmodf(hue, 1.0f);
struct rgb_t rgb = hsv_to_rgb((struct hsv_t){hue, 1.0, 1.0});
// Clear
glClearColor(rgb.r, rgb.g, rgb.b, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
// ...
SDL_GL_SwapWindow(window);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
exit:
SDL_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment