// main.c part of a RISC OS SDL tutorial by Neil White
// This tells the conmpiler we will be using the SDL suite of libraries
#include <SDL/SDL.h>
#include <SDL/SDL_gfxPrimitives.h>
#include <SDL/SDL_rotozoom.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_mixer.h>
// This includes the C standard library, for basic maths i/o functions etc.
#include <stdlib.h>
// This Includes string.h which has basic string functions.
#include <string.h>
// This includes the header file containing the backgrnd xpm
#include "xpmgfx.h"
// All images, including the actual screen area are defined as surfaces, here we define a poinetr to 'screen' which we will use as the main windows image. All other images are plotted to this s urface to be displayed on screen.
SDL_Surface *screen;
// This sets aside a surface for where we will keep the Risc World logo. And a surface we can use for the background.
SDL_Surface *background;
SDL_Surface *rwlogo;
// This sets the variable we will use to get mouse events.
SDL_MouseMotionEvent mmevent;
// int defines an integer, here for the x and y positions of the mouse.
int mousex,mousey;
// ints to represent key presses
int up_pressed,down_pressed,left_pressed,down_pressed;
// This defines unsigned 32 bit integers needed, in this case 'secs' and 'cents' for holding information about time for control of the speed of the program.
Uint32 sec, cents;
// This sets a pointer we can use for collecting data from a file on disk.
FILE *file_ptr;
// Some more integers, in this case if we set quit to 1 the code will quit, bounce factor is how much the logo will move, rwx and rwy are the coordinates of the logo and bouncing is used to tell the program if the logo is bouncing or not.
int quit = 0,bouncefactor=0,rwy=60,rwx=60,bouncing=0;
// This function is used when we want to plot an image to the screen surface SDL_Surface (in this function called image) is the image we are sending to the function, x and y are the co-ordinates we sent to the function.
void imageplot(SDL_Surface *image, int x, int y)
{
// An SDL_Rect holds the width height and x,y positions of a rectangle, here we define a SDL rectangle called pos.
SDL_Rect pos;
// Set the x and y value of the rectangle to the x and y value sent to the function
pos.x = x;
pos.y = y ;
// Now we blit (plot) the surface image into the screen surface at the x and y positions we sent to the function
SDL_BlitSurface(image, NULL, screen, &pos);
// Note- all SDL co-ordinates origin from the top left of the surface
}
// This funtion will load the images we want when called
SDL_Surface *loadimage(char *name)
{
// Define some surfaces for use within this function
SDL_Surface *surface;
SDL_Surface *image;
// Define a string to hold the location of the file we want to load
char path[256];
// In the Makefile we defined DATA_PREFIX as <rwsdlex1$dir>/ this line makes that the start opf our files location
strcpy(path, DATA_PREFIX);
// Now we add gfx/, the directory the images are stored
strcat(path, "gfx/");
// Here we add the name of the file that was sent to the function
strcat(path, name);
// Load the image into a surface
surface = IMG_Load(path);
// this is a little error trap for if we cant find the file.
if (surface == NULL)
{
fprintf(stderr, "Can't load image %s", path);
return NULL;
}
// this tells SDL what colour to treat as transparent.
// This function is the workings for the bounce mechanism.
static void bouncer(void)
{
// bouncefactor is how many pixels the logo will move each step.
if ( bouncefactor>0 )
{
// Here the logo will move up until it is less than 2 pixels from the top of the screen, then switch to going down.
if ( bouncing==1 )
{
rwy-=bouncefactor;
if (rwy<2) { bouncing=0; bouncefactor-=1; }
}
// here it will move down until the logo is over 100 pixels down the screen, then switch to going up.
if ( bouncing==0 )
{
rwy+=bouncefactor;
if (rwy>100) { bouncing=1; bouncefactor-=1; }
}
}
}
// All C programs require a main() function to run, this is where all the previously defined functions are called.
// argc and argv are for collecting any extra information on the command line, usually used for setting nosound or fullscreen, which does not apply here.
int main(int argc, char *argv[])
{
// reserve i as a counter variablefor loops
int i;
// Initialize SDL
if ( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) < 0 ) {