Bitmap wont draw

Hello,
Newbie here. I can’t seem to get my bitmap to draw. I thought it was
because I had to flip my buffer once everything was drawn but that did not
help. All I get is a black screen. It appears that the BMP is loading
successfully. Thanks in advance. The code follows.

#include “SDL.h”
#include <stdio.h>
#include <unistd.h>

SDL_Surface *load_bmp(char *fileName);

int main()
{
SDL_Surface *screen, *woods;
char myBmp[] = “Woods.bmp”;

printf("Initializing SDL.\n");

/* Initialize defaults, Video and Audio */

if (SDL_Init(SDL_INIT_VIDEO) < 0) {
	printf("Could not initialize SDL: %s.\n", SDL_GetError());
	exit(-1);
}

printf("SDL Initialized.\n");

/* 
 * Initialize the display in a 640x480 8-bit palettized mode
 * requesting a software surface
 */

screen = SDL_SetVideoMode(800, 600, 16, SDL_SWSURFACE);
if ( screen == NULL ) {
	fprintf(stderr, "Could'nt set 640x480 video mode: %s\n",

SDL_GetError());
exit(-1);
}

printf("Set 640x480 at %d bpp mode\n",

screen->format->BitsPerPixel);

/* Load graphics */
woods = load_bmp("Woods.bmp");

/* Palettized screen modes will have a default pallette
 *          * (a standard 8x8x4 color cube), but if the image is 
 *                   * palettized as well, we can use that palette

for
* * nicer color matching
* */
if ( woods->format->palette && screen->format->palette) {
SDL_SetColors(screen, woods->format->palette->colors, 0,
woods->format->palette->ncolors);
}

/* Blit onto the screen surface */
if(SDL_BlitSurface(woods, NULL, screen, NULL) != 0)
	fprintf(stderr, "BlitSurface error: %s\n", SDL_GetError());

SDL_UpdateRect(screen, 0, 0, woods->w, woods->h);

SDL_Flip(screen);

sleep(5);

SDL_Quit();

return(0);

}

SDL_Surface *load_bmp(char *fileName)
{
SDL_Surface *tmp, *surface;

/* Load the BMP file into a surface */
tmp = SDL_LoadBMP(fileName);
if (tmp == NULL) {
	fprintf(stderr, "Could'nt load %s: %s\n", fileName,

SDL_GetError());
return;
}

surface = SDL_DisplayFormat(tmp);
SDL_FreeSurface(tmp);

printf("Successfully loaded BMP file (%s)\n", fileName);

return(surface);

}

Couple of things I don’t understand why you’re doing them.

  1. What is the purpose of your load_bmp function? Why not just call
    SDL_LoadBMP or IMG_Load?
  2. Why are you doing an UpdateRect immediately followed by a Flip? Why not
    just call Flip?

I’d try substituting SDL_LoadBMP for your load_bmp to see if it then
works. If it does, you know it’s something wrong in your function (going
out of scope?).On Fri, 3 Aug 2001, Joel Dudley wrote:

Hello,
Newbie here. I can’t seem to get my bitmap to draw. I thought it was
because I had to flip my buffer once everything was drawn but that did not
help. All I get is a black screen. It appears that the BMP is loading
successfully. Thanks in advance. The code follows.

#include “SDL.h”
#include <stdio.h>
#include <unistd.h>

SDL_Surface *load_bmp(char *fileName);

int main()
{
SDL_Surface *screen, *woods;
char myBmp[] = “Woods.bmp”;

printf(“Initializing SDL.\n”);

/* Initialize defaults, Video and Audio */

if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf(“Could not initialize SDL: %s.\n”, SDL_GetError());
exit(-1);
}

printf(“SDL Initialized.\n”);

/*

  • Initialize the display in a 640x480 8-bit palettized mode
  • requesting a software surface
    */

screen = SDL_SetVideoMode(800, 600, 16, SDL_SWSURFACE);
if ( screen == NULL ) {
fprintf(stderr, “Could’nt set 640x480 video mode: %s\n”,
SDL_GetError());
exit(-1);
}

printf(“Set 640x480 at %d bpp mode\n”,
screen->format->BitsPerPixel);

/* Load graphics */
woods = load_bmp(“Woods.bmp”);

/* Palettized screen modes will have a default pallette

  •      * (a standard 8x8x4 color cube), but if the image is 
    
  •               * palettized as well, we can use that palette
    

for

  •                        * nicer color matching
    
  •                                 */
    

if ( woods->format->palette && screen->format->palette) {
SDL_SetColors(screen, woods->format->palette->colors, 0,
woods->format->palette->ncolors);
}

/* Blit onto the screen surface */
if(SDL_BlitSurface(woods, NULL, screen, NULL) != 0)
fprintf(stderr, “BlitSurface error: %s\n”, SDL_GetError());

SDL_UpdateRect(screen, 0, 0, woods->w, woods->h);

SDL_Flip(screen);

sleep(5);

SDL_Quit();

return(0);
}

SDL_Surface *load_bmp(char *fileName)
{
SDL_Surface *tmp, *surface;

/* Load the BMP file into a surface */
tmp = SDL_LoadBMP(fileName);
if (tmp == NULL) {
fprintf(stderr, “Could’nt load %s: %s\n”, fileName,
SDL_GetError());
return;
}

surface = SDL_DisplayFormat(tmp);
SDL_FreeSurface(tmp);

printf(“Successfully loaded BMP file (%s)\n”, fileName);

return(surface);
}


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Probably to do the SDL_LoadBMP and SDL_DisplaySurface all in one fell swoop.

-bill!On Fri, Aug 03, 2001 at 01:28:11PM -0500, Ti Leggett wrote:

Couple of things I don’t understand why you’re doing them.

  1. What is the purpose of your load_bmp function? Why not just call
    SDL_LoadBMP or IMG_Load?

Hmmm, I changed the code and still nothing is drawn. Since the image is
loaded the surface must exist somewhere. Why doesn’t it get drawn to the
screen with SDL_BlitSurface?? Thanks for the great responses!

  • Joel

#include “SDL.h”
#include <stdio.h>
#include <unistd.h>

SDL_Surface *load_bmp(char *fileName);

int main()
{
SDL_Surface *screen, *woods;
char myBmp[] = “Woods.bmp”;

    printf("Initializing SDL.\n");

    /* Initialize defaults, Video and Audio */

    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
            printf("Could not initialize SDL: %s.\n", SDL_GetError());
            exit(-1);
    }

    printf("SDL Initialized.\n");

    /* 
     * Initialize the display in a 640x480 8-bit palettized mode
     * requesting a software surface
     */

    screen = SDL_SetVideoMode(800, 600, 16, SDL_SWSURFACE);
    if ( screen == NULL ) {
            fprintf(stderr, "Could'nt set 800x600 video mode: %s\n",

SDL_GetError());
exit(-1);
}

    printf("Set 640x480 at %d bpp mode\n",

screen->format->BitsPerPixel);

    /* Load graphics */
    woods = SDL_LoadBMP("Woods.bmp");

    /* Palettized screen modes will have a default pallette
     *          * (a standard 8x8x4 color cube), but if the image is 
     *                   * palettized as well, we can use that palette

for
* * nicer color matching
* */
if ( woods->format->palette && screen->format->palette) {
SDL_SetColors(screen, woods->format->palette->colors, 0,
woods->format->palette->ncolors);
}

    /* Blit onto the screen surface */
    if(SDL_BlitSurface(woods, NULL, screen, NULL) != 0)
            fprintf(stderr, "BlitSurface error: %s\n", SDL_GetError());

    SDL_UpdateRect(screen, 0, 0, woods->w, woods->h);

    sleep(5);

    SDL_Quit();

    return(0);

}> ----- Original Message -----

From: Ti Leggett [mailto:leggett@eecs.tulane.edu]
Sent: Friday, August 03, 2001 11:28 AM
To: 'sdl at libsdl.org
Subject: Re: [SDL] bitmap wont draw

Couple of things I don’t understand why you’re doing them.

  1. What is the purpose of your load_bmp function? Why not just call
    SDL_LoadBMP or IMG_Load?
  2. Why are you doing an UpdateRect immediately followed by a Flip? Why not
    just call Flip?

I’d try substituting SDL_LoadBMP for your load_bmp to see if it then
works. If it does, you know it’s something wrong in your function (going
out of scope?).

On Fri, 3 Aug 2001, Joel Dudley wrote:

Hello,
Newbie here. I can’t seem to get my bitmap to draw. I thought it was
because I had to flip my buffer once everything was drawn but that did not
help. All I get is a black screen. It appears that the BMP is loading
successfully. Thanks in advance. The code follows.

#include “SDL.h”
#include <stdio.h>
#include <unistd.h>

SDL_Surface *load_bmp(char *fileName);

int main()
{
SDL_Surface *screen, *woods;
char myBmp[] = “Woods.bmp”;

printf("Initializing SDL.\n");

/* Initialize defaults, Video and Audio */

if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  printf("Could not initialize SDL: %s.\n", SDL_GetError());
  exit(-1);
}

printf("SDL Initialized.\n");

/* 
* Initialize the display in a 640x480 8-bit palettized mode
* requesting a software surface
*/

screen = SDL_SetVideoMode(800, 600, 16, SDL_SWSURFACE);
if ( screen == NULL ) {
  fprintf(stderr, "Could'nt set 640x480 video mode: %s\n",

SDL_GetError());
exit(-1);
}

printf("Set 640x480 at %d bpp mode\n",

screen->format->BitsPerPixel);

/* Load graphics */
woods = load_bmp("Woods.bmp");

/* Palettized screen modes will have a default pallette
*          * (a standard 8x8x4 color cube), but if the image is 
*                   * palettized as well, we can use that palette

for
* * nicer color matching
* */
if ( woods->format->palette && screen->format->palette) {
SDL_SetColors(screen, woods->format->palette->colors, 0,
woods->format->palette->ncolors);
}

/* Blit onto the screen surface */
if(SDL_BlitSurface(woods, NULL, screen, NULL) != 0)
  fprintf(stderr, "BlitSurface error: %s\n", SDL_GetError());

SDL_UpdateRect(screen, 0, 0, woods->w, woods->h);

SDL_Flip(screen);

sleep(5);

SDL_Quit();

return(0);

}

SDL_Surface *load_bmp(char *fileName)
{
SDL_Surface *tmp, *surface;

/* Load the BMP file into a surface */
tmp = SDL_LoadBMP(fileName);
if (tmp == NULL) {
  fprintf(stderr, "Could'nt load %s: %s\n", fileName,

SDL_GetError());
return;
}

surface = SDL_DisplayFormat(tmp);
SDL_FreeSurface(tmp);

printf("Successfully loaded BMP file (%s)\n", fileName);

return(surface);

}


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

My Bad, I had no event loop to hold things up and the process was just
happening too fast to see. Dunno why the sleep didn’t slow it up enough.
Anyway, thanks to all who replied to my message.

  • Joel> ----- Original Message -----

From: Joel Dudley [mailto:Joel.Dudley@DevelopOnline.com]
Sent: Friday, August 03, 2001 12:16 PM
To: 'sdl at libsdl.org
Subject: RE: [SDL] bitmap wont draw

Hmmm, I changed the code and still nothing is drawn. Since the image is
loaded the surface must exist somewhere. Why doesn’t it get drawn to the
screen with SDL_BlitSurface?? Thanks for the great responses!

  • Joel

#include “SDL.h”
#include <stdio.h>
#include <unistd.h>

SDL_Surface *load_bmp(char *fileName);

int main()
{
SDL_Surface *screen, *woods;
char myBmp[] = “Woods.bmp”;

    printf("Initializing SDL.\n");

    /* Initialize defaults, Video and Audio */

    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
            printf("Could not initialize SDL: %s.\n", SDL_GetError());
            exit(-1);
    }

    printf("SDL Initialized.\n");

    /* 
     * Initialize the display in a 640x480 8-bit palettized mode
     * requesting a software surface
     */

    screen = SDL_SetVideoMode(800, 600, 16, SDL_SWSURFACE);
    if ( screen == NULL ) {
            fprintf(stderr, "Could'nt set 800x600 video mode: %s\n",

SDL_GetError());
exit(-1);
}

    printf("Set 640x480 at %d bpp mode\n",

screen->format->BitsPerPixel);

    /* Load graphics */
    woods = SDL_LoadBMP("Woods.bmp");

    /* Palettized screen modes will have a default pallette
     *          * (a standard 8x8x4 color cube), but if the image is 
     *                   * palettized as well, we can use that palette

for
* * nicer color matching
* */
if ( woods->format->palette && screen->format->palette) {
SDL_SetColors(screen, woods->format->palette->colors, 0,
woods->format->palette->ncolors);
}

    /* Blit onto the screen surface */
    if(SDL_BlitSurface(woods, NULL, screen, NULL) != 0)
            fprintf(stderr, "BlitSurface error: %s\n", SDL_GetError());

    SDL_UpdateRect(screen, 0, 0, woods->w, woods->h);

    sleep(5);

    SDL_Quit();

    return(0);

}

-----Original Message-----
From: Ti Leggett [mailto:leggett@eecs.tulane.edu]
Sent: Friday, August 03, 2001 11:28 AM
To: 'sdl at libsdl.org
Subject: Re: [SDL] bitmap wont draw

Couple of things I don’t understand why you’re doing them.

  1. What is the purpose of your load_bmp function? Why not just call
    SDL_LoadBMP or IMG_Load?
  2. Why are you doing an UpdateRect immediately followed by a Flip? Why not
    just call Flip?

I’d try substituting SDL_LoadBMP for your load_bmp to see if it then
works. If it does, you know it’s something wrong in your function (going
out of scope?).

On Fri, 3 Aug 2001, Joel Dudley wrote:

Hello,
Newbie here. I can’t seem to get my bitmap to draw. I thought it was
because I had to flip my buffer once everything was drawn but that did not
help. All I get is a black screen. It appears that the BMP is loading
successfully. Thanks in advance. The code follows.

#include “SDL.h”
#include <stdio.h>
#include <unistd.h>

SDL_Surface *load_bmp(char *fileName);

int main()
{
SDL_Surface *screen, *woods;
char myBmp[] = “Woods.bmp”;

printf("Initializing SDL.\n");

/* Initialize defaults, Video and Audio */

if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  printf("Could not initialize SDL: %s.\n", SDL_GetError());
  exit(-1);
}

printf("SDL Initialized.\n");

/* 
* Initialize the display in a 640x480 8-bit palettized mode
* requesting a software surface
*/

screen = SDL_SetVideoMode(800, 600, 16, SDL_SWSURFACE);
if ( screen == NULL ) {
  fprintf(stderr, "Could'nt set 640x480 video mode: %s\n",

SDL_GetError());
exit(-1);
}

printf("Set 640x480 at %d bpp mode\n",

screen->format->BitsPerPixel);

/* Load graphics */
woods = load_bmp("Woods.bmp");

/* Palettized screen modes will have a default pallette
*          * (a standard 8x8x4 color cube), but if the image is 
*                   * palettized as well, we can use that palette

for
* * nicer color matching
* */
if ( woods->format->palette && screen->format->palette) {
SDL_SetColors(screen, woods->format->palette->colors, 0,
woods->format->palette->ncolors);
}

/* Blit onto the screen surface */
if(SDL_BlitSurface(woods, NULL, screen, NULL) != 0)
  fprintf(stderr, "BlitSurface error: %s\n", SDL_GetError());

SDL_UpdateRect(screen, 0, 0, woods->w, woods->h);

SDL_Flip(screen);

sleep(5);

SDL_Quit();

return(0);

}

SDL_Surface *load_bmp(char *fileName)
{
SDL_Surface *tmp, *surface;

/* Load the BMP file into a surface */
tmp = SDL_LoadBMP(fileName);
if (tmp == NULL) {
  fprintf(stderr, "Could'nt load %s: %s\n", fileName,

SDL_GetError());
return;
}

surface = SDL_DisplayFormat(tmp);
SDL_FreeSurface(tmp);

printf("Successfully loaded BMP file (%s)\n", fileName);

return(surface);

}


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Joel Dudley <Joel.Dudley at DevelopOnline.com> writes:

Hmmm, I changed the code and still nothing is drawn. Since the image is
loaded the surface must exist somewhere. Why doesn’t it get drawn to the
screen with SDL_BlitSurface?? Thanks for the great responses!

    screen = SDL_SetVideoMode(800, 600, 16, SDL_SWSURFACE);

Does the screen have to be a SDL_HWSURFACE?

Hmmm, I changed the code and still nothing is drawn. Since the image is
loaded the surface must exist somewhere. Why doesn’t it get drawn to the
screen with SDL_BlitSurface?? Thanks for the great responses!

> /* Blit onto the screen surface */ > if(SDL_BlitSurface(woods, NULL, screen, NULL) != 0) > fprintf(stderr, "BlitSurface error: %s\n", SDL_GetError());

I think this is the issue. The sizes (width/height) of the
source surface (woods) and your destination (screen) aren’t the same,
but you’re telling SDL_BlitSurface() to take one and fill up the entirety
of the other with it.

In other words, you should do this:

SDL_Rect dest;

dest.x = 0;
dest.y = 0;
dest.w = woods -> w;
dest.h = woods -> h;

… SDL_BlitSurface(woods, NULL, screen, &dest) != 0 …

Good luck!

-bill!On Fri, Aug 03, 2001 at 12:16:04PM -0700, Joel Dudley wrote:

I think this is the issue. The sizes (width/height) of the
source surface (woods) and your destination (screen) aren’t the same,
but you’re telling SDL_BlitSurface() to take one and fill up the entirety
of the other with it.

No, the size of the destination rectangle are ignored by BlitSurface().
This is documented.

Whew! I’m so behind the times! :slight_smile: Sorry!

-bill!On Fri, Aug 03, 2001 at 10:56:44PM +0200, Mattias Engdeg?rd wrote:

I think this is the issue. The sizes (width/height) of the
source surface (woods) and your destination (screen) aren’t the same,
but you’re telling SDL_BlitSurface() to take one and fill up the entirety
of the other with it.

No, the size of the destination rectangle are ignored by BlitSurface().
This is documented.