Sign in to follow this  
alkzy1

hmmm, debugging

Recommended Posts

alkzy1    122
I'm just starting out with a game, and I'm setting up the framework using SDL. What is really annoying me is this run time error I'm getting where the program just pops and windows goes above it and says the program has stopped working. When I ran the compile line straight into the cmd line then the same thing happens, but afterward when it try to guess what the problem is it reports that it's some virus. So I'm thinking it might be preventing the code from executing because it thinks its a threat. In specific its the getRectArr function. Anyway, it's really annoying me and I was wondering if you guys would be willing to give it a look-see. Any help is appreciated. /* ============================================================================ Name : MyGame.c Author : Alex Koeppel Version : 0.0 Copyright : Your copyright notice Description : My first game creation ============================================================================ */ #include <stdio.h> #include <stdlib.h> #include "SDL/SDL.h" #include <errno.h> #include "SDL/SDL_image.h" #define SCREEN_WIDTH 640 #define SCREEN_HEIGHT 480 #define SCREEN_BPP 32 SDL_Surface *screen = NULL; SDL_Surface* getSurface(char *fileName, unsigned char r, unsigned char b, unsigned char g) { SDL_Surface *loadedImage; loadedImage = SDL_LoadBMP(fileName); if(loadedImage != NULL) { SDL_Surface *optimizedImage; optimizedImage = SDL_DisplayFormat(loadedImage); if(optimizedImage != NULL) { SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB(optimizedImage -> format, r, b, g)); } else printf("Error!: %s", SDL_GetError()); SDL_FreeSurface(loadedImage); return optimizedImage; } else printf("Error!: %s", SDL_GetError()); return loadedImage; } SDL_Rect*** getRectArr(int rows, int cols, int spriteHeight, int spriteWidth) { SDL_Rect ***arr; arr = (SDL_Rect***) malloc(rows * cols * sizeof(SDL_Rect)); int i, j; for(i = 0; i < rows; i++) { for(j = 0; j < cols; j++) { SDL_Rect *srcPosition; srcPosition = arr[i][j]; srcPosition -> y = i * spriteHeight; srcPosition -> x = j * spriteWidth; srcPosition -> h = spriteHeight; srcPosition -> w = spriteWidth; } } return arr; } void applySurface(SDL_Surface *source, SDL_Surface *destination, SDL_Rect *srcPosition, SDL_Rect *dstPosition) { SDL_BlitSurface(source, srcPosition, destination, dstPosition); } int main(int argc, char **args) { if(SDL_Init(SDL_INIT_EVERYTHING) < 0) { printf("Error!: %s", SDL_GetError()); exit(EXIT_FAILURE); } screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_HWSURFACE); if(screen == NULL) { printf("Error!: %s", SDL_GetError()); exit(EXIT_FAILURE); } SDL_FillRect(screen, &screen -> clip_rect, SDL_MapRGB(screen -> format, 0xFF, 0xFF, 0xFF)); SDL_WM_SetCaption("Pre-Alpha Stage of Game", NULL); SDL_Rect ***rectArr; rectArr = getRectArr(4, 5, 40, 25); //when commented out along with //applySurface blank screen shows as //it should SDL_Surface *spriteSheet = getSurface("characterpicture.bmp", 0xFF, 0xFF, 0xFF); SDL_Rect destination; destination.x = 0; destination.y = 0; applySurface(spriteSheet, screen, rectArr[0][0], &destination); SDL_Flip(screen); SDL_Delay(5000); SDL_FreeSurface(spriteSheet); SDL_Quit(); return 0; }

Share this post


Link to post
Share on other sites
rip-off    10979
You can't index a pointer to a pointer to a pointer like that without a bit more work. In any case, if you want a two dimensional array then you don't need an additional layer of indirection.

Use something like this:

int index(int width, int x, int y)
{
return y * width + x;
}

SDL_Rect* getRectArr(int rows, int cols, int spriteHeight, int spriteWidth)
{
SDL_Rect *arr;
int i, j;

arr = malloc(rows * cols * sizeof(SDL_Rect));

for(i = 0; i < rows; i++)
{
for(j = 0; j < cols; j++)
{
SDL_Rect *srcPosition;
srcPosition = &arr[index(cols,i,j)];

srcPosition -> y = i * spriteHeight;
srcPosition -> x = j * spriteWidth;
srcPosition -> h = spriteHeight;
srcPosition -> w = spriteWidth;
}
}
return arr;
}


The calling code becomes:

SDL_Rect *rectArr;
rectArr = getRectArr(4, 5, 40, 25);

// ...

applySurface(spriteSheet, screen, &rectArr[index(40,0,0)], &destination);


Alternatively you could probably generate the rectangles on demand:

SDL_Rect generateRect(int x, int y, int spriteWidth, int spriteHeight)
{
SDL_Rect result;
result.x = // ...
result.y = // ...
// more ...
return result;
}


Then you would have this calling code:

SDL_Rect source = generateRect(0,0,40,25);
applySurface(spriteSheet, screen, &source, &destination);



[edit: yes, it would have to be a single dimensional array. I managed to forget to change it properly]

Share this post


Link to post
Share on other sites
KulSeran    3267
Yup. You screwed up your 2d array. 2d arrays are a pain. just do a 1D array.

SDL_Rect *arr = (SDL_Rect*) malloc(rows * cols * sizeof(SDL_Rect));

for ( int i = 0; i < rows; ++i )
for ( int j = 0; j < cols; ++j )
{
SDL_Rect *surface = &arr[i*cols + j];
// blah
}




edit. too slow.


SDL_Rect **arr = (SDL_Rect**) malloc(rows * sizeof(SDL_Rect*));
for ( int i = 0; i < rows; ++i )
arr[i] = (SDL_Rect*) malloc(cols*sizeof(SDL_Rect));
// ...
for ( int i = 0; i < rows; ++i )
for ( int j = 0; j < cols; ++j )
{
SDL_Rect *surface = &arr[i][j];
// blah
}
// ...
for ( int i = 0; i < rows; ++i
free(arr[i]);
free(arr);

Share this post


Link to post
Share on other sites
alkzy1    122
Thank you very much, I actually independently fixed it. I need to work on my pointer manipulation some. Anyway I did the following:

SDL_Rect **arr;
arr = (SDL_Rect**) malloc(rows * sizeof(SDL_Rect*));
int i, j;
for(i = 0; i < rows; i++)
{
arr[i] = (SDL_Rect*) malloc(cols * sizeof(SDL_Rect));
for(j = 0; j < cols; j++)
{
SDL_Rect srcPosition;
srcPosition.y = i * spriteHeight;
srcPosition.x = j * spriteWidth;
srcPosition.h = spriteHeight;
srcPosition.w = spriteWidth;
arr[i][j] = srcPosition;
}
}
return arr;

where arr is a pointer to a pointer to a sdl_rect, then I could either pass the rect to my other functions by either &arr[a][b] or (*(arr + a) + b). I think that should work. I like how helpful the members in the forums are.

Out of curiosity what html do you use for the embedded code box?

[Edited by - alkzy1 on December 19, 2008 2:57:13 PM]

Share this post


Link to post
Share on other sites
rip-off    10979
To get a source box, use source tags: [source]/* ... */[/source]

You get this displayed:

/* ... */


If you ever want to know how someone included something in their post, click the "edit" button on the top right of their post. You won't really be able to edit someone else's post (unless you are a moderator [wink]) but you can see what HTML or tags they used to format it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this