Initial commit
This commit is contained in:
commit
70ff7e3386
BIN
breakout/.breakouttest.c.swp
Normal file
BIN
breakout/.breakouttest.c.swp
Normal file
Binary file not shown.
BIN
breakout/breakout
Normal file
BIN
breakout/breakout
Normal file
Binary file not shown.
157
breakout/breakout.c
Normal file
157
breakout/breakout.c
Normal file
@ -0,0 +1,157 @@
|
|||||||
|
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
4
breakout/makefile
Normal file
4
breakout/makefile
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
breakout_s = breakout.c
|
||||||
|
breakout_c = breakout
|
||||||
|
all: breakout.c
|
||||||
|
clang $(breakout_s) -l SDL2 -lGLU -lGL -o $(breakout_c)
|
||||||
13
breakout/readme.md
Normal file
13
breakout/readme.md
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
|
||||||
|
|
||||||
|
Breakout
|
||||||
|
========
|
||||||
|
|
||||||
|
breakout.c
|
||||||
|
----------
|
||||||
|
|
||||||
|
> Compile using `make`
|
||||||
|
|
||||||
|
Main program file
|
||||||
|
|
||||||
|
|
||||||
93
readme.md
Normal file
93
readme.md
Normal file
@ -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)
|
||||||
23
sdlboilerplate/makefile
Normal file
23
sdlboilerplate/makefile
Normal file
@ -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)
|
||||||
135
sdlboilerplate/pointy.c
Normal file
135
sdlboilerplate/pointy.c
Normal file
@ -0,0 +1,135 @@
|
|||||||
|
|
||||||
|
// This is the main SDL include file
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
|
||||||
|
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<width-10;x+=((width-20)/20),y+=((height-20)/20))
|
||||||
|
{
|
||||||
|
SDL_RenderDrawPoint(renderer,x,y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 );
|
||||||
|
|
||||||
|
|
||||||
|
/* 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 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;
|
||||||
|
}
|
||||||
212
sdlboilerplate/pointy_modified.c
Normal file
212
sdlboilerplate/pointy_modified.c
Normal file
@ -0,0 +1,212 @@
|
|||||||
|
|
||||||
|
// This is the main SDL include file
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
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<width-1;x+=((width+100)/100),y+=((height+100)/100))
|
||||||
|
// {
|
||||||
|
// SDL_RenderDrawPoint(renderer,x,y);
|
||||||
|
// }
|
||||||
|
float ratio=(float)height/(float)width;
|
||||||
|
//printf("width: %i, height: %i, ratio: %f\n",width,height,ratio);
|
||||||
|
int loop=0;
|
||||||
|
while(x<width-1 && y<height-1) {
|
||||||
|
loop++;
|
||||||
|
x+=1; y=(int)(ratio*(float)x);
|
||||||
|
SDL_RenderDrawPoint(renderer,x,y);
|
||||||
|
} loop=0; x=0; y=0;
|
||||||
|
while(x<width-1 && y<height-1) {
|
||||||
|
loop++;
|
||||||
|
y+=1; x=(int)((float)y/ratio);
|
||||||
|
SDL_RenderDrawPoint(renderer,x,y);
|
||||||
|
} loop=0; x=0; y=0;
|
||||||
|
|
||||||
|
/* CIRCLE */
|
||||||
|
|
||||||
|
SDL_SetRenderDrawColor(renderer, 0, 255, 64, 255);
|
||||||
|
|
||||||
|
// x=width/2; y=height/2;
|
||||||
|
// float pi=3.141;
|
||||||
|
// int r=64;
|
||||||
|
|
||||||
|
// x=0;y=0;
|
||||||
|
//
|
||||||
|
// for(int i=0,j=0;i<4;i++) {
|
||||||
|
// if(i%2=0){j=1;} if(i%2=1){j=-1}; //Every other loop
|
||||||
|
// if(i<2) {k=1;} else {k=-1}; //Last two loops
|
||||||
|
// while (x<width-1 && y<height-1) {
|
||||||
|
// loop++;
|
||||||
|
// x+=1; y=sqrt(pow(r,2)-pow(x,2));
|
||||||
|
// SDL_RenderDrawPoint(renderer,x+width/2,y+height/2);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// y=plusminusSqrt(r**2-x**2)
|
||||||
|
|
||||||
|
//printf("[%i] [x,y]: [%i, %i], ratio %f\n",loop,x,y,ratio);
|
||||||
|
|
||||||
|
|
||||||
|
int radius=64;
|
||||||
|
int x0=width/2;
|
||||||
|
int y0=height/2;
|
||||||
|
x = radius-1;
|
||||||
|
y = 0;
|
||||||
|
int dx = 1;
|
||||||
|
int dy = 1;
|
||||||
|
int err = dx - (radius << 1);
|
||||||
|
|
||||||
|
while (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;
|
||||||
|
}
|
||||||
29
sdlboilerplate/readme.md
Normal file
29
sdlboilerplate/readme.md
Normal file
@ -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
|
||||||
|
|
||||||
|
|
||||||
105
sdlboilerplate/sdlboilerplate.c
Normal file
105
sdlboilerplate/sdlboilerplate.c
Normal file
@ -0,0 +1,105 @@
|
|||||||
|
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user