From 70ff7e3386da244a96fc27ce68418e214245278f Mon Sep 17 00:00:00 2001 From: Adams Joseph Date: Thu, 7 Dec 2017 14:15:17 +0000 Subject: [PATCH] Initial commit --- breakout/.breakouttest.c.swp | Bin 0 -> 12288 bytes breakout/breakout | Bin 0 -> 13264 bytes breakout/breakout.c | 157 +++++++++++++++++++++++ breakout/makefile | 4 + breakout/readme.md | 13 ++ readme.md | 93 ++++++++++++++ sdlboilerplate/makefile | 23 ++++ sdlboilerplate/pointy.c | 135 ++++++++++++++++++++ sdlboilerplate/pointy_modified.c | 212 +++++++++++++++++++++++++++++++ sdlboilerplate/readme.md | 29 +++++ sdlboilerplate/sdlboilerplate.c | 105 +++++++++++++++ 11 files changed, 771 insertions(+) create mode 100644 breakout/.breakouttest.c.swp create mode 100644 breakout/breakout create mode 100644 breakout/breakout.c create mode 100644 breakout/makefile create mode 100644 breakout/readme.md create mode 100644 readme.md create mode 100644 sdlboilerplate/makefile create mode 100644 sdlboilerplate/pointy.c create mode 100644 sdlboilerplate/pointy_modified.c create mode 100644 sdlboilerplate/readme.md create mode 100644 sdlboilerplate/sdlboilerplate.c diff --git a/breakout/.breakouttest.c.swp b/breakout/.breakouttest.c.swp new file mode 100644 index 0000000000000000000000000000000000000000..6ca269a8311b36e32ad3304d337ce2ae7f9e6a6d GIT binary patch literal 12288 zcmeI2U2GIp6vrpezq2*T>^Hu?S|5peo_c&rn|S@f!Up9X11ju zDj_i%qVWY1WAuRs-;5D78uh_9F_EZI2+?SQi4tElJ`saP|L5MB?)DQDUoYnl(Mv`WxlotiZjub6(+|u%Sy)ZsfwDf|WDH_6#7}{to8X2{OcEl_>jH{s0f zTk|b^1$+gj6(~7lq3x?!Lp&CxvaQuy_=fcZ)7AbDUjbhMUjbhMUjbhMUjbhMUjbi% z|GxrGaS3}GX)bkBebv1#nRRt<{fn=FuYj+BuYj+BuYj+BuYj+BuYj+BuYj+BufTs$ z0WHf|32`sK2PgdhzxVI|Ki4q!6SxYlfEU3F;7M=-On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kPOi1rv*;;R2#ewHGp<@Qfs8|>_8 z1F+12UJmedET(M@U)LH8Yu9brvOUzTbq8C+A&sXv%}si$katy<_k>AW#hj_~bSjzB zgliSbXzI8QN_sS|Wm1ODbjaK4is(9|i-{a4O;07X!AKktbX=aSB@(+tqLs*_3Iwd_ zQDymJqAQpgR?7Vnm`LEF9`W^>ra|r+MYU3BAu4 zyB*!nwBDpVUd5^mWt#5^ZiK>H@GeL8!!-e3H7o%?mr8c@-Y8+V84AnbNA|RSeOv^s@lLm8+46PJ&lvvw~zmJY2PXxPA&@Y(xkMf_j|O* zINR+%2N=dgsu#a+)JaA9ZUHKss2?QnhJw|e*7aU#&vO7s8F-y66F*?Hr{}36KPWU| z;4DOT1bq<-*7oUpOt(vD(02*axAz~{pJ-3kL>CFiT?k9P7GZJ)PK4Ter;=?-m_-cLIn`%3#lX%ei|yQ z{V%?cDZ=6q8A?ue#9sj1YESF6Ly9n{rM|Ypf4Ift<&yPgax%DB+(X?kx#=9 z+xMbiA+mqPf?*MLk%(hj8@0_V +#include + +void drawPaddle(SDL_Renderer *renderer, int paddleX, int paddleWidth); + +int main(void) { + + /* + * RETURN CODES + * 0: Success + * 1: SDL init fail + * 2: Error creating Window + * 3: Error creating renderer + * + */ + + /* ---------------- + * SDL Init + * ---------------- + */ + if (SDL_Init(SDL_INIT_EVERYTHING)!=0) { + perror("[FAILED] Initialising SLD"); + return 1; + } + + int go; //Var for loop control + + /* ---------------- + * Main Window + * ---------------- + * Create the main output Window for SDL + */ + + // Window vars + int winPosX = 100; int winPosY = 100; + int winWidth = 640; int winHeight = 480; + + SDL_Window *window1 = SDL_CreateWindow( + "Main Window", + winPosX, winPosY, + winWidth, winHeight, + SDL_WINDOW_SHOWN + ); + if (window1==NULL) { + perror("[FAILED] Creating Window window1"); + return 2; //Error creating Window + } + + /* + * ---------------- + * Renderer + * ---------------- + * Creates the main renderer (tied to main window) + */ + + SDL_Renderer * renderer1 = SDL_CreateRenderer(window1, -1, 0); + if (renderer1==NULL) { + perror("[FAILED] Creating renderer renderer1 for window1"); + return 3; + } + + /* + * ---------------- + * Game variables + * ---------------- + */ + + char moveX=0; + + int paddleWidth=100; + int paddleX=(winWidth/2)-(paddleWidth/2); + + SDL_SetRenderDrawColor(renderer1, 0x22, 0x22, 0x22, 0xFF); //RGBA + SDL_RenderClear(renderer1); + + go=1; + while(go) { + + /* + * ---------------- + * Event handling + * ---------------- + */ + SDL_Event incomingEvent; + while (SDL_PollEvent(&incomingEvent)) { + switch (incomingEvent.type) { + case SDL_QUIT: + // User requested program quit - e.g. window close + go=0; + break; + case SDL_KEYDOWN: + switch(incomingEvent.key.keysym.sym) { + case SDLK_ESCAPE: + go=0; + break; + case SDLK_LEFT: + moveX=-1; + break; + case SDLK_RIGHT: + moveX=1; + break; + } + break; + case SDL_KEYUP: + switch(incomingEvent.key.keysym.sym) { + case SDLK_LEFT: + moveX=0; + break; + case SDLK_RIGHT: + moveX=0; + break; + } + break; + } + } + + paddleX+=moveX*1; + + /* + * ---------------- + * Draw to buffer + * ---------------- + */ + SDL_SetRenderDrawColor(renderer1, 0x22, 0x22, 0x22, 0xFF); //RGBA + SDL_RenderClear(renderer1); + + drawPaddle(renderer1, paddleX, paddleWidth); + + /* + * -------------------------------- + * Output to Window + * -------------------------------- + */ + SDL_RenderPresent(renderer1); + SDL_Delay(6); + } + + /* + * -------------------------------- + * Clean up after ourselves + * -------------------------------- + */ + 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); + } + } +} diff --git a/breakout/makefile b/breakout/makefile new file mode 100644 index 0000000..6e9dd26 --- /dev/null +++ b/breakout/makefile @@ -0,0 +1,4 @@ +breakout_s = breakout.c +breakout_c = breakout +all: breakout.c + clang $(breakout_s) -l SDL2 -lGLU -lGL -o $(breakout_c) diff --git a/breakout/readme.md b/breakout/readme.md new file mode 100644 index 0000000..067ab92 --- /dev/null +++ b/breakout/readme.md @@ -0,0 +1,13 @@ + + +Breakout +======== + + breakout.c + ---------- + + > Compile using `make` + + Main program file + + diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..f4d5e73 --- /dev/null +++ b/readme.md @@ -0,0 +1,93 @@ +Breakout +======== + +CATA 2017 Programming Principles project: Breakout game + +Getting Started +--------------- + +To download and run, follow the instructions below. + +### Prerequisites + +**OpenGL** compatible graphics driver + +#### Clang + +*Default compiler used for this project* +See the [Clang getting started page](https://clang.llvm.org/get_started.html) for info on compiling. +Pre-build packages available on Debian/Ubuntu: + +> \# apt-get install clang + +and CentOS/Fedora: + +> \# yum install clang + +#### SDL +*Simple DirectMedia Layer library* +See the [SDL wiki page](https://wiki.libsdl.org/Installation) for installation instructions + +### Installing + +Clone the repository (into a new directory *breakout_dir*) + +> $ git clone https://bitbucket.org/sumptum/cata17-1-ef-breakout.git breakout_dir + +#### Breakout + +Change to the breakout directory inside the repository (contains the main program) +and run `make` to compile + +> $ cd breakout_dir/breakout +> $ make + +To run, execute the output binary - **breakout** (takes no args) + +> $ ./breakout + +##### Troubleshooting + +**Permission denied:** Usually this is a result of the binary not having the correct permission bits set. +The user must have permission to execute the binary, e.g. + +> $ chmod +x ./breakout + +Exit codes: + +Code | Meaning +---- | ------- +0 | Completed successfully +1 | SDL initialisation failed +2 | Error creating window +3 | Error creating renderer + +Stderr may contain more information. See source for more + +#### (Others) + +Other folders contain test and reference code, detailed below. +For build instructions, view the readme files provided within + +Running `make` with no arguments will prompt, e.g.: + +> $ cd breakout_dir/sdlboilerplate +> $ make +> .... OUTPUT TRUNCATED .... +> compile using `make pointy` +> .... OUTPUT TRUNCATED .... + +### Contents + +#### breakout + +This is the main folder for the project, containing the code for the breakout game + +#### (Others) + +Additional subfolders contain reference, experimental or tangential code used in creating neccesary components of the program. +For credits and usage details, see the readme files provided within + +### Authors + +* **Joe Adams** - *Owner* - [s5067322@bournemouth.ac.uk](mailto:s5067322@bournemouth.ac.uk) / [dev@joea.co.uk](mailto:dev@joea.co.uk) diff --git a/sdlboilerplate/makefile b/sdlboilerplate/makefile new file mode 100644 index 0000000..421c6c6 --- /dev/null +++ b/sdlboilerplate/makefile @@ -0,0 +1,23 @@ +unspecified: + cat readme.md + +pointy_s = pointy.c +pointy_c = pointy +pointy: $(pointy_s) + clang $(pointy_s) -l SDL2 -o $(pointy_c) + chmod +x $(pointy_c) + ./$(pointy_c) + +pointymod_s = pointy_modified.c +pointymod_c = pointy_modified +pointymod: $(pointymod_s) + clang $(pointymod_s) -l SDL2 -o $(pointymod_c) + chmod +x $(pointymod_c) + ./$(pointymod_c) + +boilerplate_s = sdlboilerplate.c +boilerplate_c = sdlboilerplate +boilerplate: $(boilerplate_s) + clang $(boilerplate_s) -l SDL2 -o $(boilerplate_c) + chmod +x $(boilerplate_c) + ./$(boilerplate_c) diff --git a/sdlboilerplate/pointy.c b/sdlboilerplate/pointy.c new file mode 100644 index 0000000..e86b606 --- /dev/null +++ b/sdlboilerplate/pointy.c @@ -0,0 +1,135 @@ + +// This is the main SDL include file +#include +#include +#include + + +void pointy(SDL_Renderer *renderer, int width, int height) +{ + int x,y; + SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); + for(x=10,y=10;x +#include +#include +#include + +void pointy(SDL_Renderer *renderer, int width, int height) +{ + int x=1,y=1; + SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); + // for(x=1,y=1;x= y) + { + SDL_RenderDrawPoint(renderer,x0 + x, y0 + y); + SDL_RenderDrawPoint(renderer,x0 + y, y0 + x); + SDL_RenderDrawPoint(renderer,x0 - y, y0 + x); + SDL_RenderDrawPoint(renderer,x0 - x, y0 + y); + SDL_RenderDrawPoint(renderer,x0 - x, y0 - y); + SDL_RenderDrawPoint(renderer,x0 - y, y0 - x); + SDL_RenderDrawPoint(renderer,x0 + y, y0 - x); + SDL_RenderDrawPoint(renderer,x0 + x, y0 - y); + + if (err <= 0) + { + y++; + err += dy; + dy += 2; + } + if (err > 0) + { + x--; + dx += 2; + err += dx - (radius << 1); + } + } + +} + +int main(void) +{ + /* These are some variables to help show you what the parameters are for the initialisation function. + You can experiment with the numbers to see what they do. */ + int winPosX = 100; + int winPosY = 100; + int winWidth = 640; + int winHeight = 480; + int go; + + /* This is our initialisation phase + + SDL_Init is the main initialisation function for SDL + It takes a 'flag' parameter which we use to tell SDL what systems we are going to use + Here, we want to initialise everything, so we give it the flag for this. + This function also returns an error value if something goes wrong, + so we can put this straight in an 'if' statement to check and exit if need be */ + if( SDL_Init( SDL_INIT_EVERYTHING ) != 0 ) + { + /* Something went very wrong in the initialisation, all we can do is exit */ + perror("Whoops! Something went very wrong, cannot initialise SDL :("); + return -1; + } + + /* Now we have got SDL initialised, we are ready to create a window! */ + SDL_Window *window = SDL_CreateWindow("My Pointy Window!!!", /* The first parameter is the window title */ + winPosX, winPosY, + winWidth, winHeight, + SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE); + /* The last parameter lets us specify a number of options. + Here, we tell SDL that we want the window to be shown and that it can be resized. + You can learn more about SDL_CreateWindow here: https://wiki.libsdl.org/SDL_CreateWindow?highlight=%28\bCategoryVideo\b%29|%28CategoryEnum%29|%28CategoryStruct%29 + The flags you can pass in for the last parameter are listed here: https://wiki.libsdl.org/SDL_WindowFlags + + The SDL_CreateWindow function returns an SDL_Window. + This is a structure which contains all the data about our window (size, position, etc). + We will also need this when we want to draw things to the window. + This is therefore quite important we do not lose it! */ + + /* The SDL_Renderer is a structure that handles rendering. + It will store all of SDL's internal rendering related settings. + When we create it we tell it which SDL_Window we want it to render to. + That renderer can only be used for this window (yes, this means we can have multiple windows). */ + SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0); + + + /* We are now preparing for our main loop. + This loop will keep going round until we exit from our program by changing the int 'go' to the value false (0). + This loop is an important concept and forms the basis of most SDL programs you will be writing. + Within this loop we generally do the following things: + * Check for input from the user (and do something about it!) + * Update our graphics + * Draw our graphics + */ + go = 1; + while( go ) + { + /* Here we are going to check for any input events. + Basically when you press the keyboard or move the mouse, the parameters are stored as something called an 'event' or 'message'. + SDL has a queue of events. We need to check for each event and then do something about it (called 'event handling'). + The SDL_Event is the data type for the event. */ + SDL_Event incomingEvent; + + + /* SDL_PollEvent will check if there is an event in the queue - this is the program's 'message pump'. + If there is nothing in the queue it will not sit and wait around for an event to come along (there are functions which do this, + and that can be useful too!). Instead for an empty queue it will simply return 'false' (0). + If there is an event, the function will return 'true' (!=0) and it will fill the 'incomingEvent' we have given it as a parameter with the event data */ + while( SDL_PollEvent( &incomingEvent ) ) + { + /* If we get in here, we have an event and need to figure out what to do with it. + For now, we will just use a switch based on the event's type */ + switch( incomingEvent.type ) + { + case SDL_QUIT: + /* The event type is SDL_QUIT. + This means we have been asked to quit - probably the user clicked on the 'x' at the top right corner of the window. + To quit we need to set our 'go' variable to false (0) so that we can escape out of the main loop. */ + go = 0; + break; + + /* If you want to learn more about event handling and different SDL event types, see: + https://wiki.libsdl.org/SDL_Event + and also: https://wiki.libsdl.org/SDL_EventType */ + } + } + + + + /* Draw our graphics */ + + /* Start by clearing what was drawn before */ + /* Set the colour for drawing */ + SDL_SetRenderDrawColor( renderer, 0xFF, 0xFF, 0xFF, 0xFF ); + /* Clear the entire screen to our selected colour */ + SDL_RenderClear( renderer ); + + /* Find current window size */ + SDL_GetWindowSize( window, &winWidth, &winHeight); + + /* Call our pointy drawing */ + pointy( renderer, winWidth, winHeight ); + + + /* This tells the renderer to actually show its contents to the screen. */ + SDL_RenderPresent(renderer); + + /* Turns out SDL_WaitEvent just does an SDL_PollEvent and then an SDL_Delay(10) if there is nothing to do! */ + SDL_Delay(10); + + //go=0; + } + + /* If we get outside the main loop, it means our user has requested we exit. */ + + + /* Our cleanup phase, hopefully fairly self-explanatory ;) */ + SDL_DestroyWindow( window ); + SDL_Quit(); + + return 0; +} diff --git a/sdlboilerplate/readme.md b/sdlboilerplate/readme.md new file mode 100644 index 0000000..aabd9e9 --- /dev/null +++ b/sdlboilerplate/readme.md @@ -0,0 +1,29 @@ + + +Boilerplate Code +================ + + Creating a bare-minimum C file for setting up SDL with a single window and renderer (result is **sdlboilerplate.c**) + + pointy.c + -------- + + > Compile using `make pointy` + + Original file provided by Eike Anderson as part of an exercise to introduce SDL + + pointy_modified.c + ----------------- + + > Compile using `make pointymod` + + Completed pointy.c file (creating a 'line' from rendered points that resizes with the window, as well as a circle positioned centrally) + + sdlboilerplate.c + ---------------- + + > Compile using `make boilerplate` + + A template file created using the completed **pointy.c** file as a base for abstraction + + diff --git a/sdlboilerplate/sdlboilerplate.c b/sdlboilerplate/sdlboilerplate.c new file mode 100644 index 0000000..55ef1c0 --- /dev/null +++ b/sdlboilerplate/sdlboilerplate.c @@ -0,0 +1,105 @@ + +#include +#include + +int main(void) { + + /* + * RETURN CODES + * 0: Success + * 1: SDL init fail + * 2: Error creating Window + * 3: Error creating renderer + * + */ + + /* ---------------- + * SDL Init + * ---------------- + */ + if (SDL_Init(SDL_INIT_EVERYTHING)!=0) { + perror("[FAILED] Initialising SLD"); + return 1; + } + + int go; //Var for loop control + + /* ---------------- + * Main Window + * ---------------- + * Create the main output Window for SDL + */ + + // Window vars + int winPosX = 100; int winPosY = 100; + int winWidth = 640; int winHeight = 480; + + SDL_Window *window1 = SDL_CreateWindow( + "Main Window", + winPosX, winPosY, + winWidth, winHeight, + SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE + ); + if (window1==NULL) { + perror("[FAILED] Creating Window window1"); + return 2; //Error creating Window + } + + /* + * ---------------- + * Renderer + * ---------------- + * Creates the main renderer (tied to main window) + */ + + SDL_Renderer * renderer1 = SDL_CreateRenderer(window1, -1, 0); + if (renderer1==NULL) { + perror("[FAILED] Creating renderer renderer1 for window1"); + return 3; + } + + go=1; + while(go) { + + /* + * ---------------- + * Event handling + * ---------------- + */ + SDL_Event incomingEvent; + while (SDL_PollEvent(&incomingEvent)) { + switch (incomingEvent.type) { + case SDL_QUIT: + // User requested program quit - e.g. window close + go=0; + break; + } + } + + /* + * ---------------- + * Draw to buffer + * ---------------- + */ + SDL_SetRenderDrawColor(renderer1, 0xFF, 0x00, 0x0, 0xFF); //RGBA + SDL_RenderClear(renderer1); + + /* + * -------------------------------- + * Output to Window + * -------------------------------- + */ + SDL_RenderPresent(renderer1); + SDL_Delay(10); + } + + /* + * -------------------------------- + * Clean up after ourselves + * -------------------------------- + */ + SDL_DestroyWindow(window1); + SDL_Quit(); + + return 0; +}