This repository has been archived by the owner on Apr 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
89 lines (68 loc) · 2.17 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <GLFW/glfw3.h>
#include "deps/lodepng/lodepng.h"
void keyboard_callback(GLFWwindow *window, int key, int scancode,
int action, int mods);
int main(int argc, char const *argv[])
{
GLFWwindow *window;
if (!glfwInit())
return -1;
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
window = glfwCreateWindow(400, 400, "Example Lodepng", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
unsigned int error;
unsigned char *image;
unsigned int width, height;
const char *filename = argc > 1 ? argv[1] : "textures/nav.png";
error = lodepng_decode32_file(&image, &width, &height, filename);
if (error)
printf("Error %u: %s\n", error, lodepng_error_text(error));
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, image);
free(image);
while (!glfwWindowShouldClose(window))
{
glfwSetKeyCallback(window, keyboard_callback);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 400, 400, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glClearColor(1, 1, 1, 0);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glBindTexture(GL_TEXTURE_2D, texture);
glEnable(GL_TEXTURE_2D);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glColor3ub(255, 235, 115);
glBegin(GL_POLYGON);
glTexCoord2f(0, 0); glVertex2f(50, 50);
glTexCoord2f(1, 0); glVertex2f(350, 50);
glTexCoord2f(1, 1); glVertex2f(350, 350);
glTexCoord2f(0, 1); glVertex2f(50, 350);
glEnd();
glDisable(GL_TEXTURE_2D);
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);
}