diff --git a/Makefile b/Makefile index f0605f6..35c2f7d 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,9 @@ CC=gcc -all: fps_counter +all: fps_counter rotate_cube fps_counter: $(CC) fps_counter.c -lglfw -lGL -o fps_counter + +rotate_cube: + $(CC) rotate_cube.c -lglfw -lGL -o rotate_cube.exe diff --git a/Makefile.win32 b/Makefile.win32 index 5475b84..c33f800 100644 --- a/Makefile.win32 +++ b/Makefile.win32 @@ -1,16 +1,19 @@ CC=tcc CFLAGS= -I./win32/include -L./win32/lib -all: fps_counter windows_icon +all: fps_counter rotate_cube windows_icon fps_counter: $(CC) $(CFLAGS) fps_counter.c -lglfw3 -lopengl32 -o fps_counter.exe +rotate_cube: + $(CC) $(CFLAGS) rotate_cube.c -lglfw3 -lopengl32 -o rotate_cube.exe + windows_icon: icon.o $(CC) $(CFLAGS) windows_icon.c icon.o -lglfw3 -lopengl32 -o windows_icon.exe icon.o: - windres -O coff win32/icon/resource.rc -o icon.o + windres -O coff win32/icon/resource.rc -o icon.o clean: del *.o diff --git a/images/Thumbs.db b/images/Thumbs.db new file mode 100644 index 0000000..439f3b5 Binary files /dev/null and b/images/Thumbs.db differ diff --git a/images/rotate_cube.png b/images/rotate_cube.png new file mode 100644 index 0000000..581c170 Binary files /dev/null and b/images/rotate_cube.png differ diff --git a/rotate_cube.c b/rotate_cube.c new file mode 100644 index 0000000..9c7ad72 --- /dev/null +++ b/rotate_cube.c @@ -0,0 +1,125 @@ +/* + GLFW3 rotate cube + Source: https://github.com/IIIypuk/glfw-examples +*/ + +#include +#ifndef __WIN32 + #include +#endif +#include + +void keyboard_callback(GLFWwindow *window, int key, int scancode, + int action, int mods); + +int rotate_y = 0; +int rotate_x = 0; + +#ifndef __WIN32 +int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, + LPSTR lpszCmdLine, int nShowCmd) +#else +int main(int argc, char const *argv[]) +#endif +{ + GLFWwindow *window; + + if (!glfwInit()) + return -1; + + glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); + window = glfwCreateWindow(320, 320, "Rorate Cube", NULL, NULL); + + if (!window) + { + glfwTerminate(); + return -1; + } + + glfwMakeContextCurrent(window); + + glEnable(GL_DEPTH_TEST); + + while (!glfwWindowShouldClose(window)) + { + glfwSetKeyCallback(window, keyboard_callback); + + glClearColor(0, 0, 0, 0); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glLoadIdentity(); + + glRotatef(rotate_x, 1.0, 0.0, 0.0); + glRotatef(rotate_y, 0.0, 1.0, 0.0); + + glBegin(GL_POLYGON); // Yellow side - FRONT + glColor3f(1.0, 1.0, 0.0); + 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(); + + glBegin(GL_POLYGON); // White side - BACK + glColor3f(1.0, 1.0, 1.0); + 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(); + + glBegin(GL_POLYGON); // Purple side - RIGHT + glColor3f(1.0, 0.0, 1.0); + 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(); + + glBegin(GL_POLYGON); // Green side - LEFT + glColor3f(0.0, 1.0, 0.0); + 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(); + + glBegin(GL_POLYGON); // Blue side - TOP + glColor3f(0.0, 0.0, 1.0); + 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(); + + glBegin(GL_POLYGON); // Red side - BOTTOM + glColor3f(1.0, 0.0, 0.0); + 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(); + + glfwSwapBuffers(window); + glfwPollEvents(); + } + + glfwTerminate(); + return 0; +} + +void keyboard_callback(GLFWwindow* window, int key, int scancode, + int action, int mods) +{ + if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) + glfwSetWindowShouldClose(window, GL_TRUE); + + if (key == GLFW_KEY_UP && action == GLFW_PRESS) + rotate_x += 5; + if (key == GLFW_KEY_DOWN && action == GLFW_PRESS) + rotate_x -= 5; + if (key == GLFW_KEY_RIGHT && action == GLFW_PRESS) + rotate_y -= 5; + if (key == GLFW_KEY_LEFT && action == GLFW_PRESS) + rotate_y += 5; +}