Skip to content

Commit 88070e3

Browse files
cimport macro: add basic enum processing for SOIL.h
1 parent 8fd6a37 commit 88070e3

File tree

3 files changed

+56
-24
lines changed

3 files changed

+56
-24
lines changed

demos/glfw.cx

+40-23
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,47 @@
11
module glfw;
22

3+
macro import cx.macros.assert;
34
macro import cx.macros.cimport;
45

56
import c_header("GL/gl.h");
67
import c_header("GL/glext.h", "-include GL/gl.h -DGL_GLEXT_PROTOTYPES");
78
import c_header("GLFW/glfw3.h");
9+
import c_header("SOIL.h");
810
import std.math;
911
import std.string : toStringz;
1012

11-
extern(C) void assert(bool);
12-
1313
struct Vertex {
1414
float x, y;
15-
float r, g, b;
15+
float u, v;
1616
}
1717

1818
Vertex[] vertices() {
1919
return [
20-
Vertex(-0.6, -0.4, 1, 0, 0),
21-
Vertex(0.6, -0.4, 0, 1, 0),
22-
Vertex(0, 0.6, 0, 0, 1)];
20+
Vertex(-0.6, -0.4, 1, 0),
21+
Vertex( 0.6, -0.4, 0, 0),
22+
Vertex( 0, 0.6, 0, 1)];
2323
}
2424

2525
string vertexShader() {
2626
return "#version 110
2727
uniform mat4 MVP;
28-
attribute vec3 vCol;
2928
attribute vec2 vPos;
30-
varying vec3 color;
29+
attribute vec2 vTexPos;
30+
varying vec2 texCoord;
3131
void main()
3232
{
3333
gl_Position = MVP * vec4(vPos, 0.0, 1.0);
34-
color = vCol;
34+
texCoord = vTexPos;
3535
};";
3636
}
3737

3838
string fragmentShader() {
3939
return "#version 110
40-
varying vec3 color;
40+
varying vec2 texCoord;
41+
uniform sampler2D tex;
4142
void main()
4243
{
43-
gl_FragColor = vec4(color, 1.0);
44+
gl_FragColor = texture2D(tex, texCoord);
4445
}";
4546
}
4647

@@ -139,17 +140,13 @@ mat4x4 mat4x4_ortho(float left, float right, float bottom, float top, float near
139140
}
140141

141142
void main() {
142-
GLuint vertex_shader, fragment_shader, program;
143-
144-
if (!glfwInit)
145-
return;
143+
if (!glfwInit) return;
146144

147145
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
148146
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
149147

150148
auto window = glfwCreateWindow(640, 480, "Simple example".toStringz, null, null);
151-
if (!window)
152-
{
149+
if (!window) {
153150
glfwTerminate;
154151
return;
155152
}
@@ -163,7 +160,7 @@ void main() {
163160
glGenBuffers(1, &vertex_buffer);
164161
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
165162
auto vertices = vertices;
166-
glBufferData(GL_ARRAY_BUFFER, 20 * 3 /*sizeof(vertices)*/, cast(void*) vertices.ptr, GL_STATIC_DRAW);
163+
glBufferData(GL_ARRAY_BUFFER, 16 * 3 /*sizeof(vertices)*/, cast(void*) vertices.ptr, GL_STATIC_DRAW);
167164

168165
auto vertex_shader = glCreateShader(GL_VERTEX_SHADER);
169166
char* vertexShaderPtr = vertexShader.toStringz;
@@ -174,6 +171,18 @@ void main() {
174171
char* fragmentShaderPtr = fragmentShader.toStringz;
175172
glShaderSource(fragment_shader, 1, &fragmentShaderPtr, null);
176173
glCompileShader(fragment_shader);
174+
int isCompiled;
175+
glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &isCompiled);
176+
if (!isCompiled) {
177+
int maxLength = 0;
178+
glGetShaderiv(fragment_shader, GL_INFO_LOG_LENGTH, &maxLength);
179+
180+
auto errorLog = new string(maxLength);
181+
glGetShaderInfoLog(fragment_shader, maxLength, &maxLength, errorLog.ptr);
182+
183+
print(errorLog);
184+
return;
185+
}
177186

178187
auto program = glCreateProgram;
179188
glAttachShader(program, vertex_shader);
@@ -182,14 +191,19 @@ void main() {
182191

183192
auto mvp_location = glGetUniformLocation(program, "MVP".toStringz);
184193
auto vpos_location = glGetAttribLocation(program, "vPos".toStringz);
185-
auto vcol_location = glGetAttribLocation(program, "vCol".toStringz);
194+
auto vtexpos_location = glGetAttribLocation(program, "vTexPos".toStringz);
195+
196+
auto tex = SOIL_load_OGL_texture("some_grass_or_we.png".toStringz,
197+
SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_MIPMAPS + SOIL_FLAG_INVERT_Y +
198+
SOIL_FLAG_NTSC_SAFE_RGB + SOIL_FLAG_COMPRESS_TO_DXT);
199+
assert(tex != 0);
186200

187201
glEnableVertexAttribArray(vpos_location);
188202
glVertexAttribPointer(vpos_location, 2, GL_FLOAT, false,
189-
/*sizeof(vertices[0])*/20, null);
190-
glEnableVertexAttribArray(vcol_location);
191-
glVertexAttribPointer(vcol_location, 3, GL_FLOAT, false,
192-
/*sizeof(vertices[0])*/20, cast(void*) (/*sizeof(float)*/4 * 2));
203+
/*sizeof(Vertex)*/16, null);
204+
glEnableVertexAttribArray(vtexpos_location);
205+
glVertexAttribPointer(vtexpos_location, 2, GL_FLOAT, false,
206+
/*sizeof(Vertex)*/16, cast(void*) (/*sizeof(float)*/4 * 2));
193207

194208
while (!glfwWindowShouldClose(window)) {
195209
int width, height;
@@ -205,6 +219,9 @@ void main() {
205219

206220
glUseProgram(program);
207221
glUniformMatrix4fv(mvp_location, 1, false, cast(GLfloat*) &mvp);
222+
223+
glBindTexture(GL_TEXTURE_2D, tex);
224+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
208225
glDrawArrays(GL_TRIANGLES, 0, 3);
209226

210227
glfwSwapBuffers(window);

demos/some_grass_or_we.png

1.92 KB
Loading

src/cx/macros/cimport.cx

+16-1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,21 @@ class CImportMacro : Macro
114114
// cparser.expect("\n");
115115
continue;
116116
}
117+
if (cparser.accept("enum")) {
118+
cparser.expect("{");
119+
while (true) {
120+
auto name = cparser.parseIdentifier;
121+
if (!name.length) break;
122+
if (cparser.accept("=")) {
123+
auto num = cparserHelper.parseCNumber;
124+
num.case {
125+
(:failure): break;
126+
(:success, int i): add(name, new ASTIntLiteral(i, loc));
127+
}
128+
}
129+
cparser.accept(",");
130+
}
131+
}
117132
if (cparser.accept("typedef")) {
118133
auto type = cparserHelper.parseType;
119134
if (!type) { eatline; continue; }
@@ -351,7 +366,7 @@ class CParserHelper {
351366
(:success, int i): num = i;
352367
}
353368
// weird C shit
354-
if (accept("e") || accept("E") || accept(".") || accept(",")) {
369+
if (accept("e") || accept("E") || accept(".")) {
355370
parser.revert;
356371
return :failure;
357372
}

0 commit comments

Comments
 (0)