I have nearly got them to compile... one issue remains.
tile.h
#ifndef TILE_H_#define TILE_H_#include"IO.h"// #include"player.h"// class IO;class Tile{private: int x; int y; SDL_Rect tileBox; int type; IO mIO;public: enum {TOTAL_TILES = 192}; enum { TILE_RED = 0, TILE_GREEN = 1, TILE_BLUE = 2, TILE_CENTER = 3, TILE_TOP = 4, TILE_TOPRIGHT = 5, TILE_RIGHT = 6, TILE_BOTTOMRIGHT = 7, TILE_BOTTOM = 8, TILE_BOTTOMLEFT = 9, TILE_LEFT = 10, TILE_TOPLEFT = 11 }; enum { PLAYER_WIDTH = 96, PLAYER_HEIGHT = 166, LEVEL_WIDTH = 1760, LEVEL_HEIGHT = 1320, TILE_WIDTH = 110, TILE_HEIGHT = 110, TILE_SPRITES = 12 }; Tile(); Tile( int x, int y, int tileType ); void show(); int get_type(); SDL_Rect get_box(); bool touches_wall( SDL_Rect box, Tile *tiles[] ); };#endif
tile.cpp
#include"tile.h"Tile::Tile(){ }Tile::Tile( int x, int y, int tileType ){ tileBox.x = x; tileBox.y = y; tileBox.w = TILE_WIDTH; tileBox.h = TILE_HEIGHT; type = tileType;}void Tile::show(){ if( mIO.check_collision( mIO.camera, tileBox) == true ) { mIO.apply_surface( tileBox.x - mIO.camera.x, tileBox.y - mIO.camera.y, mIO.game, mIO.screen, &mIO.clips[ type ] ); }}int Tile::get_type(){ return type;}SDL_Rect Tile::get_box(){ return tileBox;}bool Tile::touches_wall( SDL_Rect box, Tile *tiles[] ){ for( int t = 0; t < TOTAL_TILES; t++ ) { if( ( tiles[ t ]->get_type() >= TILE_TOP ) && ( tiles[ t ]->get_type() <= TILE_TOPLEFT )) { if( mIO.check_collision( box, tiles[ t ]->get_box() ) == true ) { return true; } } } return false;}
IO.h
#ifndef IO_H_#define IO_H_#include"SDL.h"#include"SDL_image.h"#include"SDL_ttf.h"#include<iostream>#include<fstream>#include<string>#include"tile.h"class Tile;class IO{private:public: enum { TILE_RED = 0, TILE_GREEN = 1, TILE_BLUE = 2, TILE_CENTER = 3, TILE_TOP = 4, TILE_TOPRIGHT = 5, TILE_RIGHT = 6, TILE_BOTTOMRIGHT = 7, TILE_BOTTOM = 8, TILE_BOTTOMLEFT = 9, TILE_LEFT = 10, TILE_TOPLEFT = 11 }; enum { PLAYER_WIDTH = 96, PLAYER_HEIGHT = 166, LEVEL_WIDTH = 1760, LEVEL_HEIGHT = 1320, TILE_WIDTH = 110, TILE_HEIGHT = 110, TILE_SPRITES = 12 }; enum {TOTAL_TILES = 192}; SDL_Surface *game; SDL_Surface *player; SDL_Surface *screen; SDL_Color textColor; SDL_Rect camera; SDL_Rect clips[ TILE_SPRITES ]; static const int SCREEN_WIDTH = 640; static const int SCREEN_HEIGHT = 480; static const int PLAYER_VELOCITY = 600; static const int SCREEN_BPP = 32; IO(); SDL_Surface* load_image( std::string filename ); void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL); bool check_collision( SDL_Rect A, SDL_Rect B ); bool init(); bool load_files(); void clean_up( Tile *tiles[] ); void clip_tiles(); bool set_tiles( Tile *tiles[] ); bool touches_wall( SDL_Rect box, Tile *tiles[] ); void draw_text( SDL_Surface *destination, std::string msg, int x, int y, int size, SDL_Color color ); };#endif
IO.cpp
#include"IO.h"IO::IO(){ SDL_Color textColor = { 0, 0, 0 }; SDL_Rect camera = { 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT };}SDL_Surface* IO::load_image( std::string filename ){ SDL_Surface* loadedImage = NULL; SDL_Surface* optimizedImage = NULL; loadedImage = IMG_Load ( filename.c_str() ); if ( loadedImage != NULL ) { optimizedImage = SDL_DisplayFormat( loadedImage ); SDL_FreeSurface( loadedImage ); if ( optimizedImage != NULL ) { SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0xA3, 0x4D, 0xFD ) ); } } return optimizedImage;}void IO::apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip){ SDL_Rect offset; offset.x = x; offset.y = y; SDL_BlitSurface( source, clip, destination, &offset );}bool IO::check_collision( SDL_Rect A, SDL_Rect B ){ int leftA, leftB; int rightA, rightB; int topA, topB; int bottomA, bottomB; leftA = A.x; rightA = A.x + A.w; topA = A.y; bottomA = A.y + A.h; leftB = B.x; rightB = B.x + B.w; topB = B.y; bottomB = B.y + B.h; if ( bottomA <= topB ) return false; if ( topA >= bottomB ) return false; if ( rightA <= leftB ) return false; if ( leftA >= rightB ) return false; return true;}bool IO::init(){ if ( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ) { return false; } screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE ); if ( screen == NULL ) { return false; } if( TTF_Init() == -1 ) { return false; } SDL_WM_SetCaption( " Kickin' Sticks - Frame Independant Movement ", NULL ); return true;}bool IO::load_files(){ game = load_image( "GFX/tiles.png" ); if ( game == NULL ) { return false; } player = load_image( "GFX/stick.png" ); if ( player == NULL ) { return false; } return true;}void IO::clean_up( Tile *tiles[] ){ SDL_FreeSurface( game ); SDL_FreeSurface( player ); for( int t = 0; t < TOTAL_TILES; t++ ) { delete tiles[ t ]; } TTF_Quit(); SDL_Quit();}bool IO::set_tiles( Tile *tiles[] ){ int x = 0, y = 0; std::ifstream map( "GFX/lazy.map" ); if ( map == NULL ) { return false; } for ( int t = 0; t < TOTAL_TILES; t++ ) { int tileType = -1; map >> tileType; if( map.fail() == true ) { map.close(); return false; } if( ( tileType >=0 ) && ( tileType < TILE_SPRITES ) ) { tiles[ t ] = new Tile( x, y, tileType ); } else { map.close(); return false; } x += TILE_WIDTH; if( x >= LEVEL_WIDTH ) { x = 0; y += TILE_HEIGHT; } } map.close(); return true;}void IO::draw_text( SDL_Surface *destination, std::string msg, int x, int y, int size, SDL_Color color ){ TTF_Font *font; font = TTF_OpenFont( "GFX/8-PM___.ttf", size ); if ( !font ) std::cerr<<"Error loading 8-PM___.ttf"<< std::endl; SDL_Rect coordinates; coordinates.x = (int)x; coordinates.y = (int)y; SDL_Surface *message = NULL; message = TTF_RenderText_Solid( font, msg.c_str(), textColor ); SDL_BlitSurface( message, NULL, destination, &coordinates ); TTF_CloseFont( font ); SDL_FreeSurface( message );}void IO::clip_tiles(){ // Clip the tile sprites for the level clips[ TILE_RED ].x = 0; clips[ TILE_RED ].y = TILE_HEIGHT * 3; clips[ TILE_RED ].w = TILE_WIDTH; clips[ TILE_RED ].h = TILE_HEIGHT; clips[ TILE_GREEN ].x = TILE_WIDTH; clips[ TILE_GREEN ].y = TILE_HEIGHT * 3; clips[ TILE_GREEN ].w = TILE_WIDTH; clips[ TILE_GREEN ].h = TILE_HEIGHT; clips[ TILE_BLUE ].x = TILE_WIDTH * 2; clips[ TILE_BLUE ].y = TILE_HEIGHT * 3; clips[ TILE_BLUE ].w = TILE_WIDTH; clips[ TILE_BLUE ].h = TILE_HEIGHT; clips[ TILE_TOPLEFT ].x = 0; clips[ TILE_TOPLEFT ].y = 0; clips[ TILE_TOPLEFT ].w = TILE_WIDTH; clips[ TILE_TOPLEFT ].h = TILE_HEIGHT; clips[ TILE_LEFT ].x = 0; clips[ TILE_LEFT ].y = TILE_HEIGHT; clips[ TILE_LEFT ].w = TILE_WIDTH; clips[ TILE_LEFT ].h = TILE_HEIGHT; clips[ TILE_BOTTOMLEFT ].x = 0; clips[ TILE_BOTTOMLEFT ].y = TILE_HEIGHT * 2; clips[ TILE_BOTTOMLEFT ].w = TILE_WIDTH; clips[ TILE_BOTTOMLEFT ].h = TILE_HEIGHT; clips[ TILE_TOP ].x = TILE_WIDTH; clips[ TILE_TOP ].y = 0; clips[ TILE_TOP ].w = TILE_WIDTH; clips[ TILE_TOP ].h = TILE_HEIGHT; clips[ TILE_CENTER ].x = TILE_WIDTH; clips[ TILE_CENTER ].y = TILE_HEIGHT; clips[ TILE_CENTER ].w = TILE_WIDTH; clips[ TILE_CENTER ].h = TILE_HEIGHT; clips[ TILE_BOTTOM ].x = TILE_WIDTH; clips[ TILE_BOTTOM ].y = TILE_HEIGHT * 2; clips[ TILE_BOTTOM ].w = TILE_WIDTH; clips[ TILE_BOTTOM ].h = TILE_HEIGHT; clips[ TILE_TOPRIGHT ].x = TILE_WIDTH * 2; clips[ TILE_TOPRIGHT ].y = 0; clips[ TILE_TOPRIGHT ].w = TILE_WIDTH; clips[ TILE_TOPRIGHT ].h = TILE_HEIGHT; clips[ TILE_RIGHT ].x = TILE_WIDTH * 2; clips[ TILE_RIGHT ].y = TILE_HEIGHT; clips[ TILE_RIGHT ].w = TILE_WIDTH; clips[ TILE_RIGHT ].h = TILE_HEIGHT; clips[ TILE_BOTTOMRIGHT ].x = TILE_WIDTH * 2; clips[ TILE_BOTTOMRIGHT ].y = TILE_HEIGHT * 2; clips[ TILE_BOTTOMRIGHT ].w = TILE_WIDTH; clips[ TILE_BOTTOMRIGHT ].h = TILE_HEIGHT;}
Compiling IO.cpp results in:
1>c:\users\xxxxxxxx\documents\visual studio 2010\projects\framework\framework\tile.h(21): error C2146: syntax error : missing ';' before identifier 'mIO'
1>c:\users\xxxxxxx\documents\visual studio 2010\projects\framework\framework\tile.h(21): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\xxxxxxxx\documents\visual studio 2010\projects\framework\framework\tile.h(21): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Compiling Tile.cpp results in a successfull build.
If I use the ' class IO; ' declaration it complains IO is refering to a undefined class.
As far as I can see the issue is due to me trying to use class function members in other classes, I am really struggling with multiple classes and passing data to and from classes.
The study material I used only used 1 class (+header) and a main.cpp to demonstrate classes each time... which is pretty easy.
Can you call on other classes functions from another class ?
Do I have to rewrite the classes so no fuction calls on any function outside the class ?
Is there any online material I can refrence that will assist me in learning to deal with multiple classes (so that I can teach myself, instead of bugging you guys constantly ) ?
I am reading that Thinking in C++ vol 1 and 2 at the moment, but would love to get over this hurdle as well... reading and doing stock samples, or counting the elements in your name samples is making me vegitate :P
Thanks again for your help.