diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main.cpp | 3 | ||||
-rw-r--r-- | src/map.cpp | 17 | ||||
-rw-r--r-- | src/renderer.cpp | 57 | ||||
-rw-r--r-- | src/renderer.h | 2 | ||||
-rw-r--r-- | src/translate_font.cpp | 32 |
5 files changed, 103 insertions, 8 deletions
diff --git a/src/main.cpp b/src/main.cpp index a2743d1..7c1a21f 100644 --- a/src/main.cpp +++ b/src/main.cpp | |||
@@ -86,6 +86,7 @@ int main() | |||
86 | 86 | ||
87 | while (!quit) | 87 | while (!quit) |
88 | { | 88 | { |
89 | /* | ||
89 | double currentTime = glfwGetTime(); | 90 | double currentTime = glfwGetTime(); |
90 | nbFrames++; | 91 | nbFrames++; |
91 | if ( currentTime - lastTime >= 1.0 ){ // If last prinf() was more than 1 sec ago | 92 | if ( currentTime - lastTime >= 1.0 ){ // If last prinf() was more than 1 sec ago |
@@ -93,7 +94,7 @@ int main() | |||
93 | printf("%f ms/frame\n", 1000.0/double(nbFrames)); | 94 | printf("%f ms/frame\n", 1000.0/double(nbFrames)); |
94 | nbFrames = 0; | 95 | nbFrames = 0; |
95 | lastTime += 1.0; | 96 | lastTime += 1.0; |
96 | } | 97 | }*/ |
97 | 98 | ||
98 | if (holding_left && player->x_vel >= 0) | 99 | if (holding_left && player->x_vel >= 0) |
99 | { | 100 | { |
diff --git a/src/map.cpp b/src/map.cpp index 10cd313..0dd3e04 100644 --- a/src/map.cpp +++ b/src/map.cpp | |||
@@ -21,9 +21,9 @@ Map::Map() | |||
21 | add_collision(GAME_WIDTH+6, 0, GAME_WIDTH, right, 1); | 21 | add_collision(GAME_WIDTH+6, 0, GAME_WIDTH, right, 1); |
22 | 22 | ||
23 | FILE* f = fopen("../maps/bigmap.txt", "r"); | 23 | FILE* f = fopen("../maps/bigmap.txt", "r"); |
24 | char* mapbuf = (char*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(char)); | 24 | char* mapbuf = (char*) malloc(MAP_WIDTH*(MAP_HEIGHT-1)*sizeof(char)); |
25 | 25 | ||
26 | for (int i=0; i<MAP_HEIGHT; i++) | 26 | for (int i=0; i<MAP_HEIGHT-1; i++) |
27 | { | 27 | { |
28 | fread(mapbuf + i*MAP_WIDTH, sizeof(char), MAP_WIDTH, f); | 28 | fread(mapbuf + i*MAP_WIDTH, sizeof(char), MAP_WIDTH, f); |
29 | fgetc(f); | 29 | fgetc(f); |
@@ -35,7 +35,7 @@ Map::Map() | |||
35 | bg = createTexture(GAME_WIDTH, GAME_HEIGHT); | 35 | bg = createTexture(GAME_WIDTH, GAME_HEIGHT); |
36 | fillTexture(bg, NULL, 0, 0, 0); | 36 | fillTexture(bg, NULL, 0, 0, 0); |
37 | 37 | ||
38 | for (int i=0; i<MAP_WIDTH*MAP_HEIGHT; i++) | 38 | for (int i=0; i<MAP_WIDTH*(MAP_HEIGHT-1); i++) |
39 | { | 39 | { |
40 | int x = i % MAP_WIDTH; | 40 | int x = i % MAP_WIDTH; |
41 | int y = i / MAP_WIDTH; | 41 | int y = i / MAP_WIDTH; |
@@ -78,6 +78,17 @@ Map::Map() | |||
78 | } | 78 | } |
79 | } | 79 | } |
80 | 80 | ||
81 | Texture* font = loadTextureFromBMP("../res/font.bmp"); | ||
82 | const char* map_name = "Everything Is Embarassing"; | ||
83 | int start_x = (40/2) - (strlen(map_name)/2); | ||
84 | for (int i=0; i<strlen(map_name); i++) | ||
85 | { | ||
86 | Rectangle srcRect(map_name[i] % 16 * 8, map_name[i] / 16 * 8, 8, 8); | ||
87 | Rectangle dstRect((start_x + i)*8, 24*8, 8, 8); | ||
88 | blitTexture(font, bg, &srcRect, &dstRect); | ||
89 | } | ||
90 | |||
91 | destroyTexture(font); | ||
81 | //destroyTexture(tiles); | 92 | //destroyTexture(tiles); |
82 | } | 93 | } |
83 | 94 | ||
diff --git a/src/renderer.cpp b/src/renderer.cpp index e74c6a4..ca356f3 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp | |||
@@ -345,6 +345,36 @@ Texture* loadTextureFromBMP(char* filename) | |||
345 | return tex; | 345 | return tex; |
346 | } | 346 | } |
347 | 347 | ||
348 | void saveTextureToBMP(Texture* tex, char* filename) | ||
349 | { | ||
350 | if (!rendererInitialized) | ||
351 | { | ||
352 | fprintf(stderr, "Renderer not initialized\n"); | ||
353 | exit(-1); | ||
354 | } | ||
355 | |||
356 | int size = 54 + 3*tex->width*tex->height; | ||
357 | |||
358 | char* buf = (char*) calloc(size, sizeof(char)); | ||
359 | buf[0x00] = 'B'; | ||
360 | buf[0x01] = 'M'; | ||
361 | *(int*)&(buf[0x0A]) = 54; | ||
362 | *(int*)&(buf[0x12]) = tex->width; | ||
363 | *(int*)&(buf[0x16]) = tex->height; | ||
364 | *(int*)&(buf[0x1C]) = 24; | ||
365 | *(int*)&(buf[0x1E]) = 0; | ||
366 | *(int*)&(buf[0x22]) = size; | ||
367 | |||
368 | glBindTexture(GL_TEXTURE_2D, tex->texID); | ||
369 | glGetTexImage(GL_TEXTURE_2D, 0, GL_BGR, GL_UNSIGNED_BYTE, buf + 54); | ||
370 | |||
371 | FILE* f = fopen(filename, "wb"); | ||
372 | fwrite(buf, sizeof(char), size, f); | ||
373 | fclose(f); | ||
374 | |||
375 | free(buf); | ||
376 | } | ||
377 | |||
348 | void fillTexture(Texture* tex, Rectangle* dstrect, int r, int g, int b) | 378 | void fillTexture(Texture* tex, Rectangle* dstrect, int r, int g, int b) |
349 | { | 379 | { |
350 | if (!rendererInitialized) | 380 | if (!rendererInitialized) |
@@ -478,15 +508,17 @@ void renderScreen(Texture* tex) | |||
478 | exit(-1); | 508 | exit(-1); |
479 | } | 509 | } |
480 | 510 | ||
481 | // Set up framebuffer | 511 | // First we're going to composite our frame with the previous frame |
512 | // We start by setting up the framebuffer | ||
482 | glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName); | 513 | glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName); |
483 | glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, renderedTexBufs[curBuf], 0); | 514 | glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, renderedTexBufs[curBuf], 0); |
484 | 515 | ||
485 | // Set up renderer | 516 | // Set up the shaer |
486 | glViewport(0,0,GAME_WIDTH,GAME_HEIGHT); | 517 | glViewport(0,0,GAME_WIDTH,GAME_HEIGHT); |
487 | glClear(GL_COLOR_BUFFER_BIT); | 518 | glClear(GL_COLOR_BUFFER_BIT); |
488 | glUseProgram(ntscShader); | 519 | glUseProgram(ntscShader); |
489 | 520 | ||
521 | // Use the current frame texture, nearest neighbor and clamped to edge | ||
490 | glActiveTexture(GL_TEXTURE0); | 522 | glActiveTexture(GL_TEXTURE0); |
491 | glBindTexture(GL_TEXTURE_2D, tex->texID); | 523 | glBindTexture(GL_TEXTURE_2D, tex->texID); |
492 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | 524 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
@@ -495,6 +527,7 @@ void renderScreen(Texture* tex) | |||
495 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | 527 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
496 | glUniform1i(glGetUniformLocation(ntscShader, "curFrameSampler"), 0); | 528 | glUniform1i(glGetUniformLocation(ntscShader, "curFrameSampler"), 0); |
497 | 529 | ||
530 | // Use the previous frame composite texture, nearest neighbor and clamped to edge | ||
498 | glActiveTexture(GL_TEXTURE1); | 531 | glActiveTexture(GL_TEXTURE1); |
499 | glBindTexture(GL_TEXTURE_2D, renderedTexBufs[(curBuf + 1) % 2]); | 532 | glBindTexture(GL_TEXTURE_2D, renderedTexBufs[(curBuf + 1) % 2]); |
500 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | 533 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
@@ -503,12 +536,13 @@ void renderScreen(Texture* tex) | |||
503 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | 536 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
504 | glUniform1i(glGetUniformLocation(ntscShader, "prevFrameSampler"), 1); | 537 | glUniform1i(glGetUniformLocation(ntscShader, "prevFrameSampler"), 1); |
505 | 538 | ||
539 | // Load the NTSC artifact texture | ||
506 | glActiveTexture(GL_TEXTURE2); | 540 | glActiveTexture(GL_TEXTURE2); |
507 | glBindTexture(GL_TEXTURE_2D, artifactsTex); | 541 | glBindTexture(GL_TEXTURE_2D, artifactsTex); |
508 | glUniform1i(glGetUniformLocation(ntscShader, "NTSCArtifactSampler"), 2); | 542 | glUniform1i(glGetUniformLocation(ntscShader, "NTSCArtifactSampler"), 2); |
509 | |||
510 | glUniform1f(glGetUniformLocation(ntscShader, "NTSCLerp"), curBuf * 1.0); | 543 | glUniform1f(glGetUniformLocation(ntscShader, "NTSCLerp"), curBuf * 1.0); |
511 | 544 | ||
545 | // Load the vertices of a flat surface | ||
512 | GLfloat g_quad_vertex_buffer_data[] = { | 546 | GLfloat g_quad_vertex_buffer_data[] = { |
513 | -1.0f, -1.0f, 0.0f, | 547 | -1.0f, -1.0f, 0.0f, |
514 | 1.0f, -1.0f, 0.0f, | 548 | 1.0f, -1.0f, 0.0f, |
@@ -523,11 +557,13 @@ void renderScreen(Texture* tex) | |||
523 | glBindBuffer(GL_ARRAY_BUFFER, quad_vertexbuffer); | 557 | glBindBuffer(GL_ARRAY_BUFFER, quad_vertexbuffer); |
524 | glBufferData(GL_ARRAY_BUFFER, sizeof(g_quad_vertex_buffer_data), g_quad_vertex_buffer_data, GL_STATIC_DRAW); | 558 | glBufferData(GL_ARRAY_BUFFER, sizeof(g_quad_vertex_buffer_data), g_quad_vertex_buffer_data, GL_STATIC_DRAW); |
525 | 559 | ||
560 | // Render our composition | ||
526 | glEnableVertexAttribArray(0); | 561 | glEnableVertexAttribArray(0); |
527 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); | 562 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); |
528 | glDrawArrays(GL_TRIANGLES, 0, 6); | 563 | glDrawArrays(GL_TRIANGLES, 0, 6); |
529 | glDisableVertexAttribArray(0); | 564 | glDisableVertexAttribArray(0); |
530 | 565 | ||
566 | // Load the normal vertices of a flat surface | ||
531 | GLfloat g_norms_data[] = { | 567 | GLfloat g_norms_data[] = { |
532 | 0.0f, 0.0f, 1.0f, | 568 | 0.0f, 0.0f, 1.0f, |
533 | 0.0f, 0.0f, 1.0f, | 569 | 0.0f, 0.0f, 1.0f, |
@@ -542,10 +578,13 @@ void renderScreen(Texture* tex) | |||
542 | glBindBuffer(GL_ARRAY_BUFFER, g_norms); | 578 | glBindBuffer(GL_ARRAY_BUFFER, g_norms); |
543 | glBufferData(GL_ARRAY_BUFFER, sizeof(g_norms_data), g_norms_data, GL_STATIC_DRAW); | 579 | glBufferData(GL_ARRAY_BUFFER, sizeof(g_norms_data), g_norms_data, GL_STATIC_DRAW); |
544 | 580 | ||
581 | // We're going to output to the window now | ||
545 | glBindFramebuffer(GL_FRAMEBUFFER, 0); | 582 | glBindFramebuffer(GL_FRAMEBUFFER, 0); |
546 | glViewport(0,0,1024,768); | 583 | glViewport(0,0,1024,768); |
547 | glClear(GL_COLOR_BUFFER_BIT); | 584 | glClear(GL_COLOR_BUFFER_BIT); |
548 | glUseProgram(finalShader); | 585 | glUseProgram(finalShader); |
586 | |||
587 | // Use the composited frame texture, linearly filtered and filling in black for the border | ||
549 | glActiveTexture(GL_TEXTURE0); | 588 | glActiveTexture(GL_TEXTURE0); |
550 | glBindTexture(GL_TEXTURE_2D, renderedTexBufs[curBuf]); | 589 | glBindTexture(GL_TEXTURE_2D, renderedTexBufs[curBuf]); |
551 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | 590 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
@@ -557,10 +596,20 @@ void renderScreen(Texture* tex) | |||
557 | glGenerateMipmap(GL_TEXTURE_2D); | 596 | glGenerateMipmap(GL_TEXTURE_2D); |
558 | glUniform1i(glGetUniformLocation(finalShader, "rendertex"), 0); | 597 | glUniform1i(glGetUniformLocation(finalShader, "rendertex"), 0); |
559 | 598 | ||
599 | // Use the scanlines texture | ||
560 | glActiveTexture(GL_TEXTURE1); | 600 | glActiveTexture(GL_TEXTURE1); |
561 | glBindTexture(GL_TEXTURE_2D, scanlinesTex); | 601 | glBindTexture(GL_TEXTURE_2D, scanlinesTex); |
562 | glUniform1i(glGetUniformLocation(finalShader, "scanlinestex"), 1); | 602 | glUniform1i(glGetUniformLocation(finalShader, "scanlinestex"), 1); |
563 | 603 | ||
604 | // Initialize the MVP matrices | ||
605 | mat4 p_matrix = perspective(90.0f, 4.0f / 4.0f, 0.1f, 100.0f); | ||
606 | mat4 v_matrix = lookAt(vec3(0,0,1), vec3(0,0,0), vec3(0,1,0)); | ||
607 | mat4 m_matrix = mat4(1.0f); | ||
608 | mat4 mvp_matrix = p_matrix * v_matrix * m_matrix; | ||
609 | |||
610 | glUniformMatrix4fv(glGetUniformLocation(finalShader, "MVP"), 1, GL_FALSE, &mvp_matrix[0][0]); | ||
611 | glUniformMatrix4fv(glGetUniformLocation(finalShader, "worldMat"), 1, GL_FALSE, &m_matrix[0][0]); | ||
612 | |||
564 | glEnableVertexAttribArray(0); | 613 | glEnableVertexAttribArray(0); |
565 | glBindBuffer(GL_ARRAY_BUFFER, quad_vertexbuffer); | 614 | glBindBuffer(GL_ARRAY_BUFFER, quad_vertexbuffer); |
566 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); | 615 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); |
diff --git a/src/renderer.h b/src/renderer.h index de5fc31..057337c 100644 --- a/src/renderer.h +++ b/src/renderer.h | |||
@@ -1,6 +1,7 @@ | |||
1 | #include <GL/glew.h> | 1 | #include <GL/glew.h> |
2 | #include <GLFW/glfw3.h> | 2 | #include <GLFW/glfw3.h> |
3 | #include <glm/glm.hpp> | 3 | #include <glm/glm.hpp> |
4 | #include <glm/gtc/matrix_transform.hpp> | ||
4 | 5 | ||
5 | using namespace glm; | 6 | using namespace glm; |
6 | 7 | ||
@@ -35,6 +36,7 @@ void destroyRenderer(); | |||
35 | Texture* createTexture(int width, int height); | 36 | Texture* createTexture(int width, int height); |
36 | void destroyTexture(Texture* tex); | 37 | void destroyTexture(Texture* tex); |
37 | Texture* loadTextureFromBMP(char* filename); | 38 | Texture* loadTextureFromBMP(char* filename); |
39 | void saveTextureToBMP(Texture* tex, char* filename); | ||
38 | void fillTexture(Texture* tex, Rectangle* loc, int r, int g, int b); | 40 | void fillTexture(Texture* tex, Rectangle* loc, int r, int g, int b); |
39 | void blitTexture(Texture* srctex, Texture* dsttex, Rectangle* srcrect, Rectangle* dstrect); | 41 | void blitTexture(Texture* srctex, Texture* dsttex, Rectangle* srcrect, Rectangle* dstrect); |
40 | void renderScreen(Texture* tex); | 42 | void renderScreen(Texture* tex); |
diff --git a/src/translate_font.cpp b/src/translate_font.cpp new file mode 100644 index 0000000..7eb4c59 --- /dev/null +++ b/src/translate_font.cpp | |||
@@ -0,0 +1,32 @@ | |||
1 | #include "renderer.h" | ||
2 | #include <cstdio> | ||
3 | |||
4 | #define min(x,y) ((x) > (y) ? (y) : (x)) | ||
5 | |||
6 | int main() | ||
7 | { | ||
8 | initRenderer(); | ||
9 | |||
10 | Texture* palette = createTexture(128, 128); | ||
11 | fillTexture(palette, NULL, 0, 0, 0); | ||
12 | |||
13 | Texture* wrong = loadTextureFromBMP("../res/arcadepix_regular_8.bmp"); | ||
14 | |||
15 | FILE* desc = fopen("../res/arcadepix_regular_8.sfl", "r"); | ||
16 | while (!feof(desc)) | ||
17 | { | ||
18 | int ch; | ||
19 | Rectangle srcRect; | ||
20 | |||
21 | fscanf(desc, "%d %d %d %d %d %*d %*d %*d \n", &ch, &(srcRect.x), &(srcRect.y), &(srcRect.w), &(srcRect.h)); | ||
22 | |||
23 | Rectangle dstRect(ch % 16 * 8, ch / 16 * 8 + (8 - min(srcRect.h,8)), min(srcRect.w, 8), min(srcRect.h, 8)); | ||
24 | blitTexture(wrong, palette, &srcRect, &dstRect); | ||
25 | } | ||
26 | |||
27 | fclose(desc); | ||
28 | |||
29 | saveTextureToBMP(palette, "tex.bmp"); | ||
30 | |||
31 | destroyRenderer(); | ||
32 | } | ||