ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi! I am attempting to make an destructable terrain demo using sdl. I made the maps using gimp and saved as .pnm in raw format. Right now the program loaded the images, and using pixel functions, it draws the image. The problem is that it doesnt turn out right. At all. Using 2 test maps, 1 (a 640x480 map) turns out somewhat familiar (but not at all right), and a 60x40 map is totally wrong.
here are screen shots to demonstrate the results: 640x480 map 60x40 map
*the application is DeterraGen. The results are in the deterragen window, and the map is in the gimp window
** the first image isn't completely up to date. The current one will not rotate the image, however it still looks the same (the interlace look that only shows about 1/4 of the picture)
Here is my image loading and printing functions, as well as my pixel function (pixel function from a tutorial, so if it looks familiar, thats why)
Code:
int map::load( const char* path )
{
unsigned long wloaded = 0, hloaded = 0;
ifstream mapfile(path, ios::binary );
if( !mapfile )
{
return 0;
}
mapfile.ignore(255, '\n' );
mapfile.ignore(255, '\n' );
mapfile>> width >> height;
cout<<"width: "<<width<<" height: "<<height<<endl;
data = new terrain*[width];
cout<<"map data pointer array initilized.\n";
for(int i = 0; i< height; i++)
data[i] = new terrain[width];
mapfile.ignore(255, '\n' );
for( int y = 0; y<height; y++)
{
for( int x = 0; x<width; x++ )
{
char buffer;
buffer = mapfile.get();
if( (unsigned short) buffer == 0 ) data[y][x].exists = 0;
else
{
data[y][x].exists = 1;
}
if( wloaded==0 )hloaded++;
}
wloaded++;
}
cout<<wloaded<<"x"<<hloaded<<" loaded.\n";
mapfile.close();
return 1;
}
void map::display( SDL_Surface *screen )
{
#ifdef DEBUG
cout<<sizeof(terrain)<<endl;
unsigned short hprinted =0, wprinted =0;
#endif
for( int y = 0; y < height; y++ )
{
for( int x = 0; x < width; x++ )
{
// cout<<x+1<<", "<<y+1<<": "<<data[x][y].exists<<endl;
if( data[y][x].exists )
{
drawPixel( screen, x, y, 255, 255, 255 );
}
else if( !data[y][x].exists )
{
drawPixel( screen, x, y, 0, 0, 0 );
}
#ifdef DEBUG
if(wprinted==0) hprinted++;
#endif
}
#ifdef DEBUG
wprinted++;
#endif
}
#ifdef DEBUG
cout<<wprinted<<"x"<<hprinted<<" printed.\n";
#endif
}
Code:
void drawPixel ( SDL_Surface *screen, int x, int y, Uint8 R, Uint8 G, Uint8 B )
{
if( SDL_MUSTLOCK( screen ) ) SDL_LockSurface( screen );
Uint32 color = SDL_MapRGB(screen->format, R, G, B);
switch (screen->format->BytesPerPixel)
{
case 1: // Assuming 8-bpp
{
Uint8 *bufp;
bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
*bufp = color;
}
break;
case 2: // Probably 15-bpp or 16-bpp
{
Uint16 *bufp;
bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
*bufp = color;
}
break;
case 3: // Slow 24-bpp mode, usually not used
{
Uint8 *bufp;
bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3;
if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
{
bufp[0] = color;
bufp[1] = color >> 8;
bufp[2] = color >> 16;
} else
{
bufp[2] = color;
bufp[1] = color >> 8;
bufp[0] = color >> 16;
}
}
break;
case 4: // Probably 32-bpp
{
Uint32 *bufp;
bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
*bufp = color;
}
break;
}
if( SDL_MUSTLOCK( screen ) ) SDL_UnlockSurface( screen );
}
I have spent many hours on this, and it has been VERY frustrating. Can anyone see why the weird results are produced? Can anyone help me fix it?
To make your image use the same pixel format the screen has:
temp = SDL_DisplayFormat(screen);
I wish you success.
And don't use fstream, ofstream and ifstream in writing binary data. You'll get them corrupt even if you use open(filename, ios::binary);
If you don't believe me try this:
ofstream foo;
ifstream bar;
char b[100];
int n;
double f;
bar = open("file.data", ios::binary);
bar>>b;
bar>>n;
bar>>f;
bar.close();
cout<<"\n"<<b<<n<<f;
What did u see?U can even use bar.get(b,13);bar>>n;bar>>f;
Take a look to the "binary" file file.data. Open it with a text editor. How do u see the int number and the double number written in the file?They appear like binary data?No, they were written like text data even if you used ios::binary flag.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.