Hey!
Jeg sidder og er igang med at lave et SDL tile spil. Problemet er den nægter at udføre koden korrekt.
Hele koden: (map.h indeholder Map array)
/*****************************
Includes / Defines
*****************************/
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include "map.h"
#define MAP_SIZEX 10
#define MAP_SIZEY 10
/*****************************
Prototypes
*****************************/
void DrawIMG(SDL_Surface *img, int x, int y); // Draw an BMP to the screen
int InitImages(); // Init the surfaces to store bmp's
void DrawScene(SDL_Surface *screen); // Draw everthing to the screen
void DrawMap(SDL_Surface *screen); // Draw the map to the screen
/****************************
Surfaces
****************************/
SDL_Surface *Tile[2];
SDL_Surface *screen;
/****************************
DrawIMG()
****************************/
void DrawIMG(SDL_Surface *img, int x, int y)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_BlitSurface(img, NULL, screen, &dest);
}
/*****************************
Init Surfaces (load bitmaps)
*****************************/
int InitImages()
{
Tile[0] = SDL_LoadBMP("gfx/t0.bmp");//Tile 0 (Grass)
Tile[1] = SDL_LoadBMP("gfx/t1.bmp");//Tile 1 (Dirt)
return 0;
}
/*****************************
Draw Map
*****************************/
void DrawMap(SDL_Surface *screen)
{
int tile;
int xcount;
int ycount;
int xpos;
int ypos;
for (int y = 0; y < MAP_SIZEY; y++)
{
for (int x = 0; x < MAP_SIZEX; x++)
{
tile = map[y][x];
xcount = xcount + 1;
//calc position
xpos = xcount * 25;
ypos = ycount * 25;
DrawIMG(Tile[tile], xpos, ypos );
}
}
}
/*****************************
Draw Scene
*****************************/
void DrawScene(SDL_Surface *screen)
{
SDL_FillRect( screen, NULL, 0x000000 );
DrawMap(screen);
SDL_Flip(screen);
}
/*****************************
Main
*****************************/
int main(int argc, char *argv[])
{
if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
{
printf("Unable to init SDL: %s\\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
screen=SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
if ( screen == NULL )
{
printf("Unable to set 640x480 video: %s\\n", SDL_GetError());
exit(1);
}
int done=0;
// Thing that shall be done BEFORE entering main loop goes here:
InitImages();
while(done == 0)
{
SDL_Event event;
while ( SDL_PollEvent(&event) )
{
if ( event.type == SDL_QUIT ) { done = 1; }
if ( event.type == SDL_KEYDOWN )
{
if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
}
}
DrawScene(screen);
}
return 0;
}
Koden som skabte problemerne: (taget ud fra det første)
void DrawMap(SDL_Surface *screen)
{
int tile;
int xcount;
int ycount;
int xpos;
int ypos;
for (int y = 0; y < MAP_SIZEY; y++)
{
for (int x = 0; x < MAP_SIZEX; x++)
{
tile = map[y][x];
xcount = xcount + 1;
//calc position
xpos = xcount * 25;
ypos = ycount * 25;
DrawIMG(Tile[tile], xpos, ypos );
}
}
}
Programmet kan sagtens tegne en Tile hvis du beder om det i DrawScene. Men Den tegner intet i DrawMap()! Jeg forstår det vrikelig ikke. Den kompiler fint. (Dev-cpp)
Help!
Indlæg senest redigeret d. 07.06.2006 22:24 af Bruger #5910
Dette ser ud til at virke:
[pre]
/*****************************
Includes / Defines
*****************************/
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <windows.h>
#define MAP_SIZEX 10
#define MAP_SIZEY 10
int map[MAP_SIZEY][MAP_SIZEX] =
{
{0,1,0,0,1,0,1,0,1,0},
{0,1,0,0,1,0,1,0,1,0},
{0,1,0,0,1,0,1,0,1,0},
{0,1,0,0,1,0,1,0,1,0},
{0,1,0,0,1,0,1,0,1,0},
{0,1,0,0,1,0,1,0,1,0},
{0,1,0,0,1,0,1,0,1,0},
{0,1,0,0,1,0,1,0,1,0},
{0,1,0,0,1,0,1,0,1,0},
{0,1,0,0,1,0,1,0,1,0}
};
/*****************************
Prototypes
*****************************/
void DrawIMG(SDL_Surface *img, int x, int y); // Draw an BMP to the screen
int InitImages(); // Init the surfaces to store bmp's
void DrawScene(SDL_Surface *screen); // Draw everthing to the screen
void DrawMap(SDL_Surface *screen); // Draw the map to the screen
/****************************
Surfaces
****************************/
SDL_Surface *Tile[2];
SDL_Surface *screen;
/****************************
DrawIMG()
****************************/
void DrawIMG(SDL_Surface *img, int x, int y, SDL_Surface *aScreen)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_BlitSurface(img, NULL, aScreen, &dest);
}
/*****************************
Init Surfaces (load bitmaps)
*****************************/
int InitImages()
{
Tile[0] = SDL_LoadBMP("ButtonOn.bmp");//Tile 0 (Grass)
Tile[1] = SDL_LoadBMP("ButtonOff.bmp");//Tile 1 (Dirt)
return 0;
}
/*****************************
Draw Map
*****************************/
void DrawMap(SDL_Surface *screen)
{
int tile;
int xcount;
int xpos;
int ypos;
for (int y = 0; y < MAP_SIZEY; y++)
{
for (int x = 0; x < MAP_SIZEX; x++)
{
tile = map[y][x];
xcount = xcount + 1;
//calc position
xpos = x * 25;
ypos = y * 25;
DrawIMG(Tile[tile], xpos, ypos, screen);
}
}
}
/*****************************
Draw Scene
*****************************/
void DrawScene(SDL_Surface *screen)
{
SDL_FillRect( screen, NULL, 0x000000 );
DrawMap(screen);
SDL_Flip(screen);
}
/*****************************
Main
*****************************/
// int main(int argc, char *argv[])
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
{
printf("Unable to init SDL: %s\\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
screen=SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
if ( screen == NULL )
{
printf("Unable to set 640x480 video: %s\\n", SDL_GetError());
exit(1);
}
int done=0;
// Thing that shall be done BEFORE entering main loop goes here:
InitImages();
while(done == 0)
{
SDL_Event event;
while ( SDL_PollEvent(&event) )
{
if ( event.type == SDL_QUIT ) { done = 1; }
if ( event.type == SDL_KEYDOWN )
{
if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
}
}
DrawScene(screen);
}
return 0;
}
[/pe]