-
Notifications
You must be signed in to change notification settings - Fork 1
/
cube.c
106 lines (79 loc) · 2.48 KB
/
cube.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <SDL.h>
#include <GL/gl.h>
// gcc cube.c $(sdl2-config --cflags --libs ) -lGL
/*
* Rotate a multicolored cube. One side changes colors
*/
int main() {
SDL_Window *window;
SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO);
window = SDL_CreateWindow(
"An SDL2 window", // window title
SDL_WINDOWPOS_UNDEFINED, // initial x position
SDL_WINDOWPOS_UNDEFINED, // initial y position
0, // width, in pixels
0, // height, in pixels
SDL_WINDOW_OPENGL|SDL_WINDOW_FULLSCREEN_DESKTOP // flags - see below
);
SDL_GLContext context = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, context);
int w,h;
SDL_GetWindowSize(window, &w, &h);
glViewport(0,0,w,h);
glEnable(GL_DEPTH_TEST);
int color = 0;
while(1){
SDL_PumpEvents();
glRotatef(1,5,5,5);
glBegin(GL_QUADS);
glColor3ub(color++ & 0x0FF, 0x86, 0xf4);
// Front face
glVertex3f(0.5,0.5,0.5);
glVertex3f(0.5,-0.5,0.5);
glVertex3f(-0.5,-0.5,0.5);
glVertex3f(-0.5,0.5,0.5);
glColor3ub(0xb9, 0xf4, 0x42);
// Back face
glVertex3f(0.5,0.5,-0.5);
glVertex3f(0.5,-0.5,-0.5);
glVertex3f(-0.5,-0.5,-0.5);
glVertex3f(-0.5,0.5,-0.5);
glColor3ub(0xef, 0x04, 0x10);
// Top face
glVertex3f(0.5,0.5,0.5);
glVertex3f(0.5,0.5,-0.5);
glVertex3f(-0.5,0.5,-0.5);
glVertex3f(-0.5,0.5,0.5);
glColor3ub(0xf1, 0xf4, 0x42);
// Bottom face
glVertex3f(0.5,-0.5,0.5);
glVertex3f(0.5,-0.5,-0.5);
glVertex3f(-0.5,-0.5,-0.5);
glVertex3f(-0.5,-0.5,0.5);
glColor3ub(0xf4, 0x42, 0xce);
// right face
glVertex3f(0.5,-0.5,0.5);
glVertex3f(0.5,-0.5,-0.5);
glVertex3f(0.5,0.5,-0.5);
glVertex3f(0.5,0.5,0.5);
glColor3ub(0xf4, 0x42, 0xce);
// left face
glVertex3f(-0.5,-0.5,0.5);
glVertex3f(-0.5,-0.5,-0.5);
glVertex3f(-0.5,0.5,-0.5);
glVertex3f(-0.5,0.5,0.5);
glEnd();
SDL_GL_SwapWindow(window);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
SDL_Event e;
while (SDL_PollEvent(&e))
{
if (e.type == SDL_QUIT)
return 0;
}
}
SDL_Delay(3000); // Pause execution for 3000 milliseconds, for example
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}