dynamic picture ?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
chrizZz
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 14
Joined: Tue Jan 27, 2009 5:12 am

dynamic picture ?

Post by chrizZz »

hi ! it's me again ^^
again i'm just screwing around with SDL and was wondering if it is possible to load a picture without giving the picture loading function its size.
i'm not sure if this is the right way doing this but i am trying to make a dynamic tileset (any size) with several dynamic arrays.
maybe someone can give me a hint ! :)
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: dynamic picture ?

Post by programmerinprogress »

if you're talking about different map sizes, then you could use a dynamic array to decide that at runtime (it would be kind of useful if you wanted to make different sized maps for different games and such)

I'm not too clear though, is this what you mean?

also, if you're talking about one picture, you could load the whole image in, and then 'clip' it to make a tileset.

in SDL, you can do this by grabbing an SDL_Rect and telling SDL to cut out a specific portion of an image, thus allowing you to adjust what user sees on the screen (a tileset in other words ;) )
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
chrizZz
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 14
Joined: Tue Jan 27, 2009 5:12 am

Re: dynamic picture ?

Post by chrizZz »

that is exactly i am trying to do but somehow i got problems by the dynamic array definition (im trieng to replace the global constants or the clip by an dynamic array) but i guess it is just coz of my incompetence that it doesn't work.
i'll try it again...maybe i'll post code in this thread later !
thanks for your reply ! :)
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: dynamic picture ?

Post by programmerinprogress »

wait a second, are we talking about two isloated situations here, or are you trying to do one thing that i've misunderstood?

basically, heres how understand it:

You want to clip sprites on a spritesheet, but you don't know exactly how big they're going to be?

Solution: you use an SDL_Rect to simply pick out the pieces of the bitmap you want by setting x,y,w and h dimensions/coordinates, no need for dynamic arrays here.

Problem two (Ithink) you want to create maps, but you don't know the sizes of those maps until runtime, you're using an array.

Solution: a static array will only accept constants, therefore you might need to use some type of a dynamic list such as a dynamic array. (i.e. a pointer to an array on the free store or heap)

Unfortunately, dynamic arrays can only be one-dimensional AFAIK, however, it's fairly easy to write a function that can turn a users two-dimensional index into a one dimension sequence.

Dynamic arrays are different, you can use variables to initialise them, after you initialise one though, you can't mess around with it's size, you cannot add or remove the number of elements like you can on say a STL vector.

I'm really a little confused with your question, but I hope I helped out a bit anyway ;)
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
chrizZz
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 14
Joined: Tue Jan 27, 2009 5:12 am

Re: dynamic picture ?

Post by chrizZz »

sorry for confusing you prgrammerinprogress but thanks for the reply !

well im troubling for hours now (got my brain fried) ^^

so i just decided to make the tileset not dynamic !
but instead of using constants for any picture(tile) of the clip i tried using an normal array.
But it still causes lots of trouble so i just decided to post the code here.

it's the code from lazy foo's site for clip blitting & sprite sheets: http://lazyfoo.net/SDL_tutorials/lesson06/index.php
just the stuff in lined red is written by me

edit: strange it doesn't affect the source code window when i mark it red so i decided to post it blanco !


#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>


//Screen Attributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;


///The Surface (3 images we're gonna use)
SDL_Surface *dots = NULL;
SDL_Surface *screen = NULL;

//The event structure that will be used
SDL_Event event;




//Tile constants
const int TILE_WIDTH = 32;
const int TILE_HEIGHT = 32;
const int TOTAL_TILES = 1200;
const int TILE_SPRITES = 1200;


//The portions of the sprite map to be blitted
SDL_Rect clip [ TILE_SPRITES ];





Ok that's the big troubling part:
green is how it was in the tutorial!
red are my ideas so just replace my red text by the original (green)




Clip range for the left top
clip[0].x = 0;
clip[0].y = 0;
clip[0].h = 100;
clip[0].w = 100;

//clip range for the right top
clip[1].x = 100;
clip[1].y = 0;
clip[1].h = 100;
clip[1].w = 100;

//clip range for the left bottom
clip[2].x = 0;
clip[2].y = 100;
clip[2].h = 100;
clip[2].w = 100;

//clip range for the right bottom
clip[3].x = 100;
clip[3].y = 100;
clip[3].h = 100;
clip[3].w = 100;




//Clip the sprite sheet
void tilefunc()
{
int x = 0;
int y = 0;
int i;
int j;


for(i=1; i<=30; y=y+32, i++)
{
for(j=1; j<=40; x=x+32, j++)
{
clip[i*(40-j)].x = x;
clip[i*(40-j)].y = y;
clip[i*(40-j)].w = TILE_WIDTH;
clip[i*(40-j)].h = TILE_HEIGHT;
}
}

}



SDL_Surface *load_image(std::string filename)
{
//Temporary storage for the image that will be used
SDL_Surface *loaded_image = NULL;
//The optimized image that will be used
SDL_Surface *optimized_image = NULL;

//load the image
loaded_image = IMG_Load(filename.c_str() );
//if nothing goes wrong while loading the image
if (loaded_image != NULL)
{
//creat an optimized image
optimized_image = SDL_DisplayFormat(loaded_image);
//free the old image
SDL_FreeSurface(loaded_image);
//if the image was optimized just fine
if (optimized_image != NULL)
{
//Map the color key
Uint32 colorkey = SDL_MapRGB(optimized_image->format, 0, 0xFF, 0xFF);
//Set all pixels of color R 0, G 0xFF, B 0xFF to be transparent
SDL_SetColorKey(optimized_image, SDL_SRCCOLORKEY, colorkey);
}
}
//return the optimized_image;
return optimized_image;
}


void apply_surface(int x, int y,SDL_Surface *source, SDL_Surface *destination, SDL_Rect *clip = NULL)
{
//Make a temporary rectangle to hold the offset
SDL_Rect offset;
//give the rectangle to the offset
offset.x = x;
offset.y = y;
//Blit the surface
SDL_BlitSurface(source, clip, destination, &offset);


}


bool Init()
{
//initialize all SDL subsystems
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
return false;
}

//setup the screen
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
//fill the screen white
SDL_FillRect(screen, &screen -> clip_rect, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF));

//if there was an error in setting up the screen
if (screen == NULL)
{
return false;
}

//Set the window caption
SDL_WM_SetCaption("EVENT TEST", NULL);

//if everything initialized fine
return true;
}

bool load_files()
{
//Load the image
dots = load_image("Tileset.png");


//if there was an error in loading the image
if (dots == NULL)
{
return false;
}

//if eerything loaded fine
return true;
}
void clean_up()
{
//Free thie image
SDL_FreeSurface(dots);

//QUITT SDL
SDL_Quit();
}








//MAIN !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
int main(int argc, char *args[]) // int main() won't work !!!
{
//make sure the programm waits for a quit
bool quit = false;

//initialize
if (Init() == false)
{
return 1;
}

//load files
if (load_files() == false)
{
return 1;
}

tilefunc();



//apply the surface to the screem
apply_surface(0, 0, dots, screen, &clip[0]); OK ! here i show 4 tiles (my Tileset.png has 1200 tiles..the very first is black and the second is red) it should be 3 black 1 red right ? but it's just 1 black :(
apply_surface(33, 0, dots, screen, &clip[0]);
apply_surface(66, 0, dots, screen, &clip[0]);
apply_surface(99, 0, dots, screen, &clip[1]);

//update the screen
if(SDL_Flip(screen) == -1)
{
return 1;
}

//while the user hasn't quit
while (quit == false)
{
//while there is an event to handle
while (SDL_PollEvent(&event))
{
//if the user Xed out the window
if (event.type == SDL_QUIT)
{

quit = true;
}
}

}

//free the surface and quit sdl
clean_up();
return 0;


}
chrizZz
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 14
Joined: Tue Jan 27, 2009 5:12 am

Re: dynamic picture ?

Post by chrizZz »

oh my god..i'm so clumsy i just realized i need to declare a 2D-ARRAY to do this...programmerinprogress even mentioned it...so sorry for my spam & have a good day (thanks for the hint programmer* ^^)
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: dynamic picture ?

Post by programmerinprogress »

you didn't spam, I just misunderstood what you were trying to accomplish (I was in two minds, I wasn't sure if you were trying to clip a sprite or create a map :lol: )

Glad you sorted it out anyway ;)
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: dynamic picture ?

Post by Ginto8 »

:roll: uhh... I'd like to ask, why on earth would you have a tilesheet with 1200 tiles? :lol:
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
chrizZz
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 14
Joined: Tue Jan 27, 2009 5:12 am

Re: dynamic picture ?

Post by chrizZz »

i use big graphics ;)
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: dynamic picture ?

Post by MarauderIIC »

PIP: You can have dynamic 2D arrays but they're a pain unless you use vectors. I think there is or was a link in the Guides & Resources on the hard way
(dynamic 2d arrays).
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
chrizZz
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 14
Joined: Tue Jan 27, 2009 5:12 am

Re: dynamic picture ?

Post by chrizZz »

hehe just got the damn dynamic array done but thank you for the tip with the ressources on the page i didn't know that !
programming can be really hard if you aren't comfortable with the language but greedy on the learning process.
but it feels so good if something just works fine after hours of troubling even if it was easy stuff :)
i really would like to show off what i've done in my first week of SDL but there are still too many structures of lazy foo's tutorial in it i am trying to get rid off.

oh and yes ! it is a matter of fact that the dynamic stuff is useless unless i have a level editor for the 1200 numbered tiles ^^;
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: dynamic picture ?

Post by eatcomics »

chrizZz wrote:hehe just got the damn dynamic array done but thank you for the tip with the ressources on the page i didn't know that !
programming can be really hard if you aren't comfortable with the language but greedy on the learning process.
but it feels so good if something just works fine after hours of troubling even if it was easy stuff :)
i really would like to show off what i've done in my first week of SDL but there are still too many structures of lazy foo's tutorial in it i am trying to get rid off.

oh and yes ! it is a matter of fact that the dynamic stuff is useless unless i have a level editor for the 1200 numbered tiles ^^;
Yeahhh... :lol:
Image
Post Reply