1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
#include "SDL.h"
#include <string>
#include <vector>
using namespace std;
//The attributes of the screen
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
const int SCREEN_BPP = 32;
SDL_Surface *message = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *tile = NULL;
SDL_Surface *load_image( std::string filename )
{
//Temporary storage for the image that's loaded
SDL_Surface* loadedImage = NULL;
//The optimized image that will be used
SDL_Surface* optimizedImage = NULL;
//Load the image
loadedImage = SDL_LoadBMP( filename.c_str() );
//If nothing went wrong in loading the image
if( loadedImage != NULL )
{
//Create an optimized image
optimizedImage = SDL_DisplayFormat( loadedImage );
//Free the old image
SDL_FreeSurface( loadedImage );
}
//Return the optimized image
return optimizedImage;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
//Make a temporary rectangle to hold the offsets
SDL_Rect offset;
//Give the offsets to the rectangle
offset.x = x;
offset.y = y;
//Blit the surface
SDL_BlitSurface( source, NULL, destination, &offset );
}
class Node {
private:
int x;
int y;
public:
//heres the Function that is supposed to apply the image to the screen.
void Node::apply(int ex, int why){
x=ex;
y=why;
apply_surface( x, y, tile, screen);}
};
void Make_Nodes(Node nodes[]){
int x = 0;
int y = 0;
//Node *n = new Node;
//Node next = n;
//node array is sized at 192
for(int i = 0; i<192; i++){
nodes[i].apply(x,y);}
//if last node was placed on the x axis with an x coordinate less than 600, next node will be placed with an coordinate 50 more than current.
if(x < 800){
x += 50;}
//if last node was placed with a y coordinate less than 600 and the last node was placed as far over on the x axis as its supposed to go(800) than next node will be placed 50 pixels lower
if(y < 600 && x == 800){
y += 50;}
//if last node was placed as far to the rigt as it was supposed to go, next node will be placed as far left as its supposed ot go.
if( x == 800){
x = 0;}
SDL_Flip( screen );
}
int main( int argc, char* args[] )
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return 1;
}
//Set up the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
//If there was an error in setting up the screen
if( screen == NULL )
{
return 1;
}
//Set the window caption
SDL_WM_SetCaption( "Hello World", NULL );
//Load the images
message = load_image( "hello.bmp" );
background = load_image( "background.bmp" );
tile = load_image("tile.bmp");
// vector<Node> nodes(192);
Node *nodes = new Node[192];
Make_Nodes(nodes);
//Update the screen
if( SDL_Flip( screen ) == -1 ){
return 1;
}
//Wait 2 seconds
SDL_Delay( 2000 );
//Free the surfaces
SDL_FreeSurface( message );
SDL_FreeSurface( background );
//Quit SDL
SDL_Quit();
//Return
return 0;
}
|