Jump to content
  • Advertisement
Sign in to follow this  
Thekill473

c++ error please help

This topic is 2560 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hello everybody right now i'm having with clipping my tile sheet. Its probably a really dumb error on my part but anyways here is the code.

graphic_functions.cpp
#include"SDL.h"
#include <string>
#include "SDL_image.h"
#include "tile_load.h"
#include "class_tile.h"

using namespace::std;

// used for clipping
SDL_Rect clips[TILE_SPRITES];

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL)
{
//Holds offsets
SDL_Rect offset;

//Get offsets
offset.x = x;
offset.y = y;

//Blit
SDL_BlitSurface( source, clip, destination, &offset );
}

void draw_sprite(int srcX, int srcY, int dstX, int dstY, int width, int height, SDL_Surface* source, SDL_Surface* destination)
{

SDL_Rect src;
src.x = srcX;
src.y = srcY;
src.w = width;
src.h = height;

SDL_Rect dst;
dst.x = dstX;
dst.y = dstY;
src.w = width;
src.h = height;

SDL_BlitSurface(source, &src, destination, &dst);

}

SDL_Surface *load_image( std::string filename )
{
//The image that's loaded
SDL_Surface* loadedImage = NULL;

//The optimized surface that will be used
SDL_Surface* optimizedImage = NULL;

//Load the image
loadedImage = IMG_Load( filename.c_str() );

//If the image loaded
if( loadedImage != NULL )
{
//Create an optimized surface
optimizedImage = SDL_DisplayFormat( loadedImage );

//Free the old surface
SDL_FreeSurface( loadedImage );

//If the surface was optimized
if( optimizedImage != NULL )
{
//Color key surface
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
}
}

//Return the optimized surface
return optimizedImage;
}


void clip_tiles()
{
//Clip the sprite sheet
clips[tile_green].x = 0;
clips[tile_green].y = 0;
clips[tile_green].w = TILE_WIDTH;
clips[tile_green].h = TILE_HEIGHT;

clips[tile_black].x = 0;
clips[tile_black].y = 32;
clips[tile_black].w = TILE_WIDTH;
clips[tile_black].h = TILE_HEIGHT;

}




please help.

Share this post


Link to post
Share on other sites
Advertisement
sorry about the double post I didn't know it posted cause I got this error with my web browser when i clicked post.
hello everybody right now I'm having with clipping my tile sheet. Its probably a really dumb error on my part but anyways here is the code.

gamedev.net mySQL query error: SELECT cs_value, cs_updated FROM cache_store WHERE cs_key='mail_processing

-_- my computer just keeps spitting errors at me

Share this post


Link to post
Share on other sites
is this the line with the error? SDL_Rect clips[TILE_SPRITES];
Array size must be fixed at compile time so you need to use a constant expression when giving the length. TILE_SPRITES should be a constant.

Share this post


Link to post
Share on other sites
Oh sorry i forgot to add the error code :P.

here is the error code.
4 IntelliSense: expression must have a constant value c:\users\tyler_2\desktop\sattriah\sattriah\sdl tutorial\graphic_functions.cpp 10 16 sdl tutorial


And here is were i declare "TILE_SPRITES".

tile_load.cpp
#include "SDL.h"
#include "graphic_functions.h"
#include "main.h"
#include "class_tile.h"
#include <iostream>
#include <fstream>
#include <string>
#include "collision.h"

//Tile constant varibles

//Tile constants
const int TILE_WIDTH = 80;
const int TILE_HEIGHT = 80;
const int TOTAL_TILES = 192;
const int TILE_SPRITES = 2;

// diffrent tile sprites
const int tile_green = 0;
const int tile_black = 1;

//the tile class

void load_tile_var(void){

Tile *tiles[TOTAL_TILES];

}

bool set_tiles(Tile *tiles[]){

//the tiles offsets
int x = 0, y = 0;

// open the map
std::ifstream map("maps//map.map");

//if the map couldnt be loaded
if(map == NULL){
return false;
}

//initialize the tiles
for(int t = 0; t < TOTAL_TILES; t++){

//what kind of tile will be made
int tiletype = -1;

//read tile from map
map >> tiletype;

//if there was a problem reading the map
if(map.fail() == true){

//stop laoding map
map.close();
return false;

}

//if the number is a valid tile
if((tiletype >= 0 ) && (tiletype < tile_sprites)){
tiles[t] = new Tile(x, y, tiletype);
}

// if we dont recognize the tile type
else{

//stop loading map
map.close();
return false;
}

//move to th next tile spot
x += TILE_WIDTH;

//if we've gone to far
if( x >= level_width){

//move back
x = 0;

//move to the next row
y += TILE_HEIGHT;

}

// clsoe file
map.close();

//if the map loaded
return true;

}
}

bool touches_wall( SDL_Rect box, Tile *tiles[] )
{
//Go through the tiles
for( int t = 0; t < TOTAL_TILES; t++ )
{
//If the tile is a wall type tile
if( ( tiles[ t ]->get_type() >= tile_green ))
{
//If the collision box touches the wall tile
if( check_collision( box, tiles[ t ]->get_box() ) == true )
{
return true;
}
}
}

//If no wall tiles were touched
return false;
}



And the header file

tile_load.h
#ifndef TILE_LOAD_H
#define TILE_LOAD_H

#include "class_tile.h"
#include "main.h"

//varibles
extern const int TILE_WIDTH;
extern const int TILE_HEIGHT;
extern const int TOTAL_TILES;
extern const int TILE_SPRITES;

extern const int tile_green;
extern const int tile_black;
extern tile *tiles[TOTAL_TILES];

//functions

extern bool set_tiles(Tile *tiles[]);
extern bool touches_wall(SDL_Rect box, Tile *tiles[]);
extern void load_tile_var(void);

#endif

Share this post


Link to post
Share on other sites
If I'm not mistaken the error message says the error is in graphic_functions.cpp which you have not posted.

Share this post


Link to post
Share on other sites
The problem is that you use extern declaration of TOTAL_TILES in the header. Put the definition of TOTAL_TILES in the header instead and it should work.

Share this post


Link to post
Share on other sites

Oh sorry i forgot to add the error code :P.

here is the error code.
4 IntelliSense: expression must have a constant value c:\users\tyler_2\desktop\sattriah\sattriah\sdl tutorial\graphic_functions.cpp 10 16 sdl tutorial


If I'm reading that correctly, and I'm counting correctly, that tells you that line 10, character 16 is where your error is at. Which makes it the TILE_SPRITES constant, as Wooh previously asked about. That would tell me that your includes, or extern, aren't being used properly. I haven't analysed the code yet, so I can't comment on that...yet.

Share this post


Link to post
Share on other sites
Your constant is of type "const int"; the compiler might not make the jump that it is perfectly constant, and will likely throw an error upon trying to use it to declare an array.

For instance, illegal pointer aliasing might cause the value to change, causing an error. Most compilers avoid this entirely and throw an error in C89; in almost all language derivatives since then, global storage _must_ be a constant value, and not a variable labeled const.

Try using #define.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!