diff --git a/breakout/breakout.c b/breakout/breakout.c index 0fcec33..156aeae 100644 --- a/breakout/breakout.c +++ b/breakout/breakout.c @@ -1,12 +1,24 @@ #ifdef _WIN32 -#include #include +#include +#define _ #else #include #endif +#include +#include #include +#include -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) { @@ -15,7 +27,7 @@ int main(void) { * 0: Success * 1: SDL init fail * 2: Error creating Window - * 3: Error creating renderer + * 3: Error creating context * */ @@ -38,13 +50,15 @@ int main(void) { // Window vars 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( "Main Window", winPosX, winPosY, winWidth, winHeight, - SDL_WINDOW_SHOWN + SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL ); if (window1==NULL) { fprintf(stderr, "[FAILED] Creating Window window1"); @@ -58,12 +72,46 @@ int main(void) { * Creates the main renderer (tied to main window) */ - SDL_Renderer * renderer1 = SDL_CreateRenderer(window1, -1, 0); - if (renderer1==NULL) { - fprintf(stderr, "[FAILED] Creating renderer renderer1 for window1"); + //SDL_Renderer * renderer1 = SDL_CreateRenderer(window1, -1, 0); + //if (renderer1==NULL) { + // 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; } + 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 @@ -72,11 +120,14 @@ int main(void) { char moveX=0; - int paddleWidth=100; + int paddleWidth=50; int paddleX=(winWidth/2)-(paddleWidth/2); - SDL_SetRenderDrawColor(renderer1, 0x22, 0x22, 0x22, 0xFF); //RGBA - SDL_RenderClear(renderer1); + /* + * ---------------- + * Let's go! + * ---------------- + */ go=1; while(go) { @@ -126,18 +177,36 @@ int main(void) { * 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 * -------------------------------- */ - SDL_RenderPresent(renderer1); - SDL_Delay(6); + SDL_GL_SwapWindow(window1); } /* @@ -145,17 +214,45 @@ int main(void) { * Clean up after ourselves * -------------------------------- */ + SDL_GL_DeleteContext(context1); SDL_DestroyWindow(window1); SDL_Quit(); return 0; } -void drawPaddle(SDL_Renderer *renderer, int paddleX, int paddleWidth) { - SDL_SetRenderDrawColor(renderer, 0x66, 0x66, 0x66, 0xFF); - for (int i=paddleX; i<(paddleX+paddleWidth); i++) { - for (int j=0; j<10; j++) { - SDL_RenderDrawPoint(renderer,i,j+480-20); - } - } +void drawBg(int winWidth, int winHeight, colour *c1, colour *c2) { + GLint matrixmode = 0; + glGetIntegerv(GL_MATRIX_MODE, &matrixmode); + glPushMatrix(); + 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); } diff --git a/design/new design.png b/design/new design.png new file mode 100644 index 0000000..5f19c93 Binary files /dev/null and b/design/new design.png differ diff --git a/design/reference.png b/design/reference.png new file mode 100644 index 0000000..f207a44 Binary files /dev/null and b/design/reference.png differ