Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "SDL/SDL.h"
- #include "GL/glew.h"
- #include <iostream>
- bool processInput();
- void draw(SDL_Window *window);
- void draw_square_over(float x, float y, float size);
- void draw_square(float x, float y, float size);
- void draw_background();
- int main(int argc, char* argv[])
- {
- SDL_Init(SDL_INIT_EVERYTHING);
- SDL_Window *window = SDL_CreateWindow("My Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1024, 768, SDL_WINDOW_OPENGL);
- if (!window)
- return 1;
- SDL_GLContext glContext = SDL_GL_CreateContext(window);
- if (!glContext)
- return 2;
- GLenum error = glewInit();
- if (error != GLEW_OK)
- return 3;
- glClearDepth(1.0);
- glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
- bool run = true;
- while (run)
- {
- run = processInput();
- draw(window);
- }
- SDL_GL_DeleteContext(glContext);
- SDL_DestroyWindow(window);
- return 0;
- }
- bool processInput()
- {
- SDL_Event event;
- while (SDL_PollEvent(&event))
- {
- switch (event.type)
- {
- case SDL_QUIT: return false;
- case SDL_MOUSEMOTION:
- std::cout << "Mouse moved to: " << event.motion.x << " " << event.motion.y << std::endl; return true;
- }
- }
- return true;
- }
- inline void draw_triangle()
- {
- glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
- glBegin(GL_TRIANGLES);
- glColor3f(1.0f, 0.0f, 0.0f); // red
- glVertex2f(-1.f, -1.f);
- glColor3f(0.0f, 1.0f, 0.0f); // blue
- glVertex2f(1.f, -1.f);
- glColor3f(0.0f, 0.0f, 1.0f); // green
- glVertex2f(1.f, 1.f);
- glEnd();
- }
- inline void draw_background() {
- draw_square_over(0.f, 0.f, 2.f);
- }
- void draw_square(float x, float y, float size) {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- draw_square_over(x, y, size);
- }
- void draw_square_over(float x, float y, float size) {
- struct {
- float x, y;
- } BL; // bottom-left
- BL.x = x - size/2;
- BL.y = y - size/2;
- glBegin(GL_QUADS);
- glColor3d(1, 0, 0);
- glVertex3f(BL.x, BL.y, 0.0f); // bottom-left
- glColor3d(1, 1, 0);
- glVertex3f(BL.x + size, BL.y, 0.0f); // bottom-right
- glColor3d(1, 1, 1);
- glVertex3f(BL.x + size, BL.y + size, 0.0f); // top-right
- glColor3d(0, 1, 1);
- glVertex3f(BL.x, BL.y + size, 0.0f); // top-left
- glEnd();
- }
- void draw(SDL_Window *window)
- {
- draw_background();
- draw_square_over(0.f, 0.f, 0.2f);
- SDL_GL_SwapWindow(window);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement