Re-wrote to use OpenGL (based off glboilerplate/glboilerplate.c), modified window size and colour scheme to match the design doc. Implemented colour struct and background drawing function

This commit is contained in:
Joe Adams 2017-12-10 19:44:04 +00:00
parent ca55281463
commit 2f7a81dd9f
3 changed files with 120 additions and 23 deletions

View File

@ -1,12 +1,24 @@
#ifdef _WIN32 #ifdef _WIN32
#include <SDL.h>
#include <Windows.h> #include <Windows.h>
#include <SDL.h>
#define _
#else #else
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#endif #endif
#include <GL/gl.h>
#include <GL/glu.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
void drawPaddle(SDL_Renderer *renderer, int paddleX, int paddleWidth); typedef struct colour {
double r;
double g;
double b;
double a;
} colour;
void drawBg(int winWidth, int winHeight, colour *c1, colour *c2);
void drawPaddle(int paddleX, int paddleWidth, int paddleHeight, colour *c);
int main(void) { int main(void) {
@ -15,7 +27,7 @@ int main(void) {
* 0: Success * 0: Success
* 1: SDL init fail * 1: SDL init fail
* 2: Error creating Window * 2: Error creating Window
* 3: Error creating renderer * 3: Error creating context
* *
*/ */
@ -38,13 +50,15 @@ int main(void) {
// Window vars // Window vars
int winPosX = 100; int winPosY = 100; int winPosX = 100; int winPosY = 100;
int winWidth = 640; int winHeight = 480; int winWidth = 480; int winHeight = 720;
Uint32 timer;
SDL_Window *window1 = SDL_CreateWindow( SDL_Window *window1 = SDL_CreateWindow(
"Main Window", "Main Window",
winPosX, winPosY, winPosX, winPosY,
winWidth, winHeight, winWidth, winHeight,
SDL_WINDOW_SHOWN SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL
); );
if (window1==NULL) { if (window1==NULL) {
fprintf(stderr, "[FAILED] Creating Window window1"); fprintf(stderr, "[FAILED] Creating Window window1");
@ -58,12 +72,46 @@ int main(void) {
* Creates the main renderer (tied to main window) * Creates the main renderer (tied to main window)
*/ */
SDL_Renderer * renderer1 = SDL_CreateRenderer(window1, -1, 0); //SDL_Renderer * renderer1 = SDL_CreateRenderer(window1, -1, 0);
if (renderer1==NULL) { //if (renderer1==NULL) {
fprintf(stderr, "[FAILED] Creating renderer renderer1 for window1"); // fprintf(stderr, "[FAILED] Creating renderer renderer1 for window1");
// return 3;
//}
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1); // Specifies the version of the OpenGL context
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 5); // (here, 1.5) -- hello, 2003!
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY); // Allows deprecated functions to be used
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); // Set depth buffer to 16 bits, if OpenGL agrees (see below)
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); // Enable double buffering
SDL_GLContext context1 = SDL_GL_CreateContext(window1); // Use SDL_GL_MakeCurrent function to switch contexts
if (context1 == NULL) {
fprintf(stderr, "[FAILED] Creating render context context1 for window1");
return 3; return 3;
} }
glFrontFace(GL_CCW); // Counter-clockwise winding
glEnable(GL_NORMALIZE); // Vectors normalized after transforming to keep units consistent for lighting
glShadeModel(GL_SMOOTH); // GL_FLAT for flat shading instead
glEnable(GL_DEPTH_TEST); // Enables depth comparisons
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glMatrixMode(GL_PROJECTION); // Sets our matrix mode (initially) to work with the projection stack
glLoadIdentity(); // Projection matrix is now the identity matrix, so we can work in 2D space
gluOrtho2D(
-1.0*(GLdouble)(winWidth / 2), // Left
(GLdouble)(winWidth / 2), // Right
-1.0*(GLdouble)(winHeight / 2), // Bottom
(GLdouble)(winHeight / 2) // Top
);
glViewport(0, 0, winWidth, winHeight); // Set the viewport to fill the window
timer = SDL_GetTicks();
/* /*
* ---------------- * ----------------
* Game variables * Game variables
@ -72,11 +120,14 @@ int main(void) {
char moveX=0; char moveX=0;
int paddleWidth=100; int paddleWidth=50;
int paddleX=(winWidth/2)-(paddleWidth/2); int paddleX=(winWidth/2)-(paddleWidth/2);
SDL_SetRenderDrawColor(renderer1, 0x22, 0x22, 0x22, 0xFF); //RGBA /*
SDL_RenderClear(renderer1); * ----------------
* Let's go!
* ----------------
*/
go=1; go=1;
while(go) { while(go) {
@ -126,18 +177,36 @@ int main(void) {
* Draw to buffer * Draw to buffer
* ---------------- * ----------------
*/ */
SDL_SetRenderDrawColor(renderer1, 0x22, 0x22, 0x22, 0xFF); //RGBA
SDL_RenderClear(renderer1);
drawPaddle(renderer1, paddleX, paddleWidth); SDL_GL_MakeCurrent(window1, context1);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
colour paddleCol;
paddleCol.r = 1.0; paddleCol.g = 1.0; paddleCol.b = 1.0; paddleCol.a = 1.0;
colour bgcol1; colour bgcol2;
bgcol1.r = 0.0824; /*21*/ bgcol2.r = 0.7216; /*184*/
bgcol1.g = 0.0901; /*23*/ bgcol2.g = 0.2471; /*63*/
bgcol1.b = 0.4431; /*113*/ bgcol2.b = 0.6078; /*155*/
bgcol1.a = 1.0000; /*255*/ bgcol2.a = 1.0000; /*255*/
drawPaddle(paddleX, paddleWidth, winWidth, winHeight, &paddleCol);
drawBg(winWidth, winHeight, &bgcol1, &bgcol2);
glFlush();
/* /*
* -------------------------------- * --------------------------------
* Output to Window * Output to Window
* -------------------------------- * --------------------------------
*/ */
SDL_RenderPresent(renderer1); SDL_GL_SwapWindow(window1);
SDL_Delay(6);
} }
/* /*
@ -145,17 +214,45 @@ int main(void) {
* Clean up after ourselves * Clean up after ourselves
* -------------------------------- * --------------------------------
*/ */
SDL_GL_DeleteContext(context1);
SDL_DestroyWindow(window1); SDL_DestroyWindow(window1);
SDL_Quit(); SDL_Quit();
return 0; return 0;
} }
void drawPaddle(SDL_Renderer *renderer, int paddleX, int paddleWidth) { void drawBg(int winWidth, int winHeight, colour *c1, colour *c2) {
SDL_SetRenderDrawColor(renderer, 0x66, 0x66, 0x66, 0xFF); GLint matrixmode = 0;
for (int i=paddleX; i<(paddleX+paddleWidth); i++) { glGetIntegerv(GL_MATRIX_MODE, &matrixmode);
for (int j=0; j<10; j++) { glPushMatrix();
SDL_RenderDrawPoint(renderer,i,j+480-20); glTranslated((GLdouble)(-1*winWidth/2), (GLdouble)(-1*winHeight/2), 0.0);
}
} glBegin(GL_QUADS);
//Top colour
glColor3f(c1->r, c1->g, c1->b);
glVertex2f(winWidth, 0.0);
glVertex2f(0.0,0.0);
//Bottom colour
glColor3f(c2->r, c2->g, c2->b);
glVertex2f(0.0,winHeight);
glVertex2f(winWidth, winHeight);
glEnd();
}
void drawPaddle(int paddleX, int paddleWidth, int winWidth, int winHeight, colour *c) {
GLint matrixmode = 0;
glGetIntegerv(GL_MATRIX_MODE, &matrixmode);
glPushMatrix();
glTranslated((GLdouble)(paddleX)-winWidth/2, (GLdouble)(20-winHeight/2), 0.0);
glBegin(GL_QUADS);
glColor3f(c->r, c->g, c->b); //#666666
glVertex3d(0.0, 0.0, 0.0);
glVertex3d(0.0, 10.0, 0.0);
glVertex3d((double)paddleWidth, 10.0, 0.0);
glVertex3d((double)paddleWidth, 0.0, 0.0);
glEnd();
glPopMatrix();
glMatrixMode(matrixmode);
} }

BIN
design/new design.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 102 KiB

BIN
design/reference.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 350 KiB