Tile drawing, why doesn't it work?

Started by
7 comments, last by ZadrraS 20 years ago
Heres the function to draw something:

void Draw(SDL_Surface *img, int x, int y) {
	SDL_Rect dest;
	dest.x;
	dest.y;
	SDL_BlitSurface(img, NULL, screen, &dest);
}
And this function draws the whole map:

void DrawMap() {
	for (int y=0; y<20; y++) {
		for (int x=0; x<10; x++) {
			if (map[y][x]==OPEN) {
				Draw(empty, x*40, y*40);				
			}
			else if (map[y][x]==TILE) {
				Draw(tile, x*40, y*40);
			}
		}
	}
}	
But it doesn''t work. i only get a black screen. Why? Heres the other stuff: #define OPEN 0 #define TILE 1 int map[20][10]= { {OPEN, OPEN, OPEN, OPEN, TILE, OPEN, OPEN, OPEN, OPEN, OPEN}, {OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN}, {OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN}, {OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN}, {OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN}, {OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN}, {OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN}, {OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN}, {OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN}, {OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN}, {OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN}, {OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN}, {OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN}, {OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN}, {OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN}, {OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN}, {OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN}, {OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN}, {OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN}, {OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN, OPEN}, };
Advertisement
Does it work when you just draw one tile with screen coordinates you supply manually?
No it doesn''t... and, im sorry, i was a little stupid to post without looking for a way out for myself, i found the problem:
This:
void Draw(SDL_Surface *img, int x, int y) {	SDL_Rect dest;	dest.x;	dest.y;	SDL_BlitSurface(img, NULL, screen, &dest);}

should be like this :
void Draw(SDL_Surface *img, int x, int y) {	SDL_Rect dest;	dest.x=x;	dest.y=y;	SDL_BlitSurface(img, NULL, screen, &dest);}
I have another question then, i need the tile to move 1 empty tile down every 2 secs, i never used time counting. How do i do it?
And another q. is this a good way to move a brick and clean up after it?

void SetMapValues() {	for (int y=0; y<20; y++) {		for (int x=0; x<10; x++) {			if (x==xpos && y==ypos) {					map[y][x]=TILE;				if (map[y-1][x]==TILE) {					map[y-1][x]=OPEN;				}				else if (map[y][x-1]==TILE) {					map[y][x-1]=OPEN;				}				else if (map[y][x+1]==TILE) {					map[y][x+1]=OPEN;				}				else {				}			}			else {						}		}	}}
quote:Original post by ZadrraS
I have another question then, i need the tile to move 1 empty tile down every 2 secs, i never used time counting. How do i do it?


i dont understand what your trying to do, but you want to use SDL_GetTicks(). SDL_GetTicks() returns how many milliseconds since SDL has been initialized (basically how long the program has been running). to time something, do this:

///very begining of the program, OUTSIDE of ANY loopsint time_start = SDL_GetTicks();//now start your game loopwhile(!gameover){int time_now = SDL_GetTicks();int time_passed = (time_now - time_start) / 1000;time_passed now holds how many seconds have passed since your game started. all you have to do isif(time_passed % 2 == 0){  2 seconds have passed since the last game loop}return 0;}


also, about your question on moving a brick. you have to be more descriptive on what your trying to accomplish. hope i helped

[edited by - graveyard filla on April 3, 2004 5:02:12 PM]
FTA, my 2D futuristic action MMORPG
quote:Original post by ZadrraS
And another q. is this a good way to move a brick and clean up after it?

void SetMapValues() {	for (int y=0; y<20; y++) {		for (int x=0; x<10; x++) {			if (x==xpos && y==ypos) {					map[y][x]=TILE;				if (map[y-1][x]==TILE) {					map[y-1][x]=OPEN;				}				else if (map[y][x-1]==TILE) {					map[y][x-1]=OPEN;				}				else if (map[y][x+1]==TILE) {					map[y][x+1]=OPEN;				}				else {				}			}			else {						}		}	}}



No. It''s messy, and will sometimes mess up if you have two adjacent blocks and one of them moves. Also, you''re looping through every square in your grid, but only doing anything when the xpos and ypos values match up, so the for loops are wasted.
What do you sugest?
And, how do i do collision detection?

This topic is closed to new replies.

Advertisement