Thread: Problem displaying bitmaps

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    3

    Problem displaying bitmaps

    Hi, a newbie here, was wondering why my code wasn't displaying bitmaps properly.. it compiles but it doesn't display anything on screen?


    Code:
    //main.cpp
    
    //The seagulls have AI.  They try to avoid JJ.
    //JJ shoots water, which have a fixed lifetime.
    //When you hit the seagulls, they explode and turn into
    //falling corpses.
    
    
    //system includes
    #include <windows.h>
    #include <windowsx.h>
    #include <ddraw.h>
    #include <stdio.h>
    
    //system defines
    #define WIN32_LEAN_AND_MEAN
    
    //custom includes
    #include "defines.h" //global definitions
    #include "bmp.h" //bmp file reader
    #include "timer.h" //game timer
    #include "csprite.h" //for clipped sprite class
    #include "objects.h" //for object class
    #include "objman.h" //for object manager
    #include "view.h" //for viewpoint class
    #include "random.h" //for random number generator
    
    //defines
    #define MAX_OBJECTS 32 //max number of objects in game
    
    //globals
    
    BOOL ActiveApp; //is this application active?
    
    LPDIRECTDRAW lpDirectDrawObject=NULL; //direct draw object
    LPDIRECTDRAWSURFACE lpPrimary=NULL; //primary surface
    LPDIRECTDRAWPALETTE lpPrimaryPalette; //its palette
    LPDIRECTDRAWSURFACE lpSecondary=NULL; //back buffer
    LPDIRECTDRAWPALETTE lpSecondaryPalette; //its palette
    LPDIRECTDRAWSURFACE lpBackground=NULL; //background image
    
    CTimer Timer; //game timer
    
    CBmpFileReader background; //background image
    CBmpSpriteFileReader g_cSpriteImages; //sprite images
    CBmpSpriteFileReader g_cFrgndImages; //foreground images
    
    CObjectManager ObjectManager(MAX_OBJECTS); //object manager
    
    CClippedSprite *g_pSprite[NUM_SPRITES]; //sprites
    
    CViewPoint Viewpoint; //player viewpoint
    CRandom Random; //random number generator
    
    //helper functions
    LPDIRECTDRAWPALETTE CreatePalette(LPDIRECTDRAWSURFACE surface);
    BOOL InitDirectDraw(HWND hwnd);
    HWND CreateDefaultWindow(char* name,HINSTANCE hInstance);
    
    BOOL LoadJJSprite(){
      BOOL result=TRUE;
      result=result&&g_pSprite[JJ_OBJECT]->
        load(&g_cSpriteImages,0,1,223);
      result=result&&g_pSprite[JJ_OBJECT]->
        load(&g_cSpriteImages,1,66,223);
      result=result&&g_pSprite[JJ_OBJECT]->
        load(&g_cSpriteImages,2,137,223);
      result=result&&g_pSprite[JJ_OBJECT]->
        load(&g_cSpriteImages,3,210,223);
      
      return result;
    } //LoadJJSprite
    
    BOOL LoadSEAGULLSprite(){
      BOOL result=TRUE;
      result=result&&g_pSprite[SEAGULL_OBJECT]->
        load(&g_cSpriteImages,0,105,106); //frame 0
      result=result&&g_pSprite[SEAGULL_OBJECT]->
        load(&g_cSpriteImages,1,105,106); //frame 1
      result=result&&g_pSprite[SEAGULL_OBJECT]->
        load(&g_cSpriteImages,2,105,106); //frame 2
      result=result&&g_pSprite[SEAGULL_OBJECT]->
        load(&g_cSpriteImages,3,105,106); //frame 3
      return result;
    } //LoadCrowSprite
    
    BOOL LoadFrgndSprites(){ //load foreground sprites
      BOOL result=TRUE;
      result=result&&g_pSprite[FARM_OBJECT]->
        load(&g_cFrgndImages,0,0,0); //load farm sprite
      result=result&&g_pSprite[FIELD_OBJECT]->
        load(&g_cFrgndImages,0,640,0); //load field sprite
      return result;
    } //LoadFrgndSprites
    
    BOOL LoadDEADSEAGULLSprite(){ //load dead crow
      return g_pSprite[DEADSEAGULL_OBJECT]->
        load(&g_cSpriteImages,0,199,110);
    } //LoadDeadCrowSprite
    
    BOOL LoadExplodingSEAGULLSprite(){ //load exploding crow
      BOOL result=TRUE;
      result=result&&g_pSprite[EXPLODINGSEAGULL_OBJECT]->
        load(&g_cSpriteImages,0,132,42);
      result=result&&g_pSprite[EXPLODINGSEAGULL_OBJECT]->
        load(&g_cSpriteImages,1,199,42);
      result=result&&g_pSprite[EXPLODINGSEAGULL_OBJECT]->
        load(&g_cSpriteImages,2,132,110);
      result=result&&g_pSprite[EXPLODINGSEAGULL_OBJECT]->
        load(&g_cSpriteImages,3,453,162);
      result=result&&g_pSprite[EXPLODINGSEAGULL_OBJECT]->
        load(&g_cSpriteImages,4,199,110);
      return result;
    } //LoadExplodingCrowSprite
    
    BOOL LoadWATERSprite(){ //load bullet
      return g_pSprite[WATER_OBJECT]->
        load(&g_cSpriteImages,0,291,265);
    } //LoadBulletSprite
    
    BOOL LoadImages(){ //load graphics from files to surfaces
      //get the background image
      if(!background.load("London3.bmp"))return FALSE; //read from file
      background.draw(lpBackground); //draw to background surface
      //set palettes in all surfaces
      if(!background.setpalette(lpPrimaryPalette))return FALSE;
      if(!background.setpalette(lpSecondaryPalette))return FALSE;
      //load the sprites...
      if(!g_cSpriteImages.load("sprites.bmp"))return FALSE;
      //...the plane
      
      //**********TAKEN OUT FOR NOW CLIPPING **********
    //  0g_pSprite[JJ_OBJECT]=new CClippedSprite(6,1,223);
     // if(!LoadJJSprite())return FALSE; //load plane images
      //...the crow
      //g_pSprite[SEAGULL_OBJECT]=new CClippedSprite(4,58,37);
      //if(!LoadSEAGULLSprite())return FALSE; //load crow images..the dead crow
      //g_pSprite[DEADSEAGULL_OBJECT]=new CClippedSprite(1,62,53);
      //LoadDeadSEAGULLSprite(); //load dead crow images
      //...the exploding crow
      //g_pSprite[EXPLODINGSEAGULL_OBJECT]=new CClippedSprite(6,62,53);
      //LoadExplodingSEAGULLSprite(); //load exploding crow images
      //...the bullet
      //g_pSprite[WATER_OBJECT]=new CClippedSprite(1,5,3);
      //LoadWaterSprite(); //load bullet images
      //...the foreground sprites
      //if(!g_cFrgndImages.load("farm.bmp"))return FALSE;
      //g_pSprite[FARM_OBJECT]=new CClippedSprite(1,640,162);
      //g_pSprite[FIELD_OBJECT]=new CClippedSprite(1,640,162);
      //if(!LoadFrgndSprites())return FALSE; //load foreground
      //return TRUE;
    } 
    
    //LoadImages()
    
    void CreateObjects(){
      int i;
      ObjectManager.create(FARM_OBJECT,0,SCREEN_HEIGHT-1,0,0);
      ObjectManager.create(FIELD_OBJECT,SCREEN_WIDTH,
        SCREEN_HEIGHT-1,0,0);
      for(i=0; i<8; i++)
        ObjectManager.create(SEAGULL_OBJECT,
          Random.number(0,WORLD_WIDTH-1),
          Random.number(100,400),-1,0);
      ObjectManager.set_current(
        ObjectManager.create(JJ_OBJECT,320,271,-1,0));
      for(i=0; i<8; i++)
        ObjectManager.create(SEAGULL_OBJECT,
          Random.number(0,WORLD_WIDTH-1),
          Random.number(100,400),-1,0);
    } //CreateObjects
    
    
    BOOL RestoreSurfaces(){ //restore all surfaces
      BOOL result=TRUE;
      if(SUCCEEDED(lpPrimary->Restore())) //if primary restored
        result=result&&background.draw(lpPrimary)&& //redraw image
          background.setpalette(lpPrimaryPalette); //set palette
      else return FALSE;
      if(SUCCEEDED(lpSecondary->Restore())) //if secondary restored
        result=result&&background.draw(lpSecondary)&& //redraw image
          background.setpalette(lpSecondaryPalette); //set palette
      else return FALSE;
      if(SUCCEEDED(lpBackground->Restore())) //if background restored
        result=result&&background.draw(lpBackground); //redraw image
      else return FALSE;
      if(g_pSprite[JJ_OBJECT]->Restore()) //if plane restored
        result=result&&LoadJJSprite(); //redraw image
      else return FALSE;
      if(g_pSprite[SEAGULL_OBJECT]->Restore()) //if crow restored
        result=result&&LoadSEAGULLSprite(); //redraw image
      else return FALSE;  
      if(g_pSprite[DEADSEAGULL_OBJECT]->Restore()) //if restored
        result=result&&LoadDEADSEAGULLSprite(); //redraw image
      else return FALSE;
      if(g_pSprite[EXPLODINGSEAGULL_OBJECT]->Restore()) //if restored
        result=result&&LoadExplodingSEAGULLSprite(); //redraw image
      else return FALSE;
      if(g_pSprite[WATER_OBJECT]->Restore()) //if restored
        result=result&&LoadWATERSprite(); //redraw image
      else return FALSE;
      if(g_pSprite[FARM_OBJECT]->Restore()&& //if farm and ...
        g_pSprite[FIELD_OBJECT]->Restore()) //... field restored
        result=result&&LoadFrgndSprites(); //redraw image
      else return FALSE;
      return result;
    } //RestoreSurfaces
    
    BOOL PageFlip(){ //return TRUE if page flip succeeds
      if(lpPrimary->Flip(NULL,DDFLIP_WAIT)==DDERR_SURFACELOST)
        return RestoreSurfaces();
      return TRUE;
    } //PageFlip
    
    BOOL ComposeFrame(){ //compose a frame of animation
      Viewpoint.draw_background(lpBackground,lpSecondary,
        ObjectManager.speed()); //draw scrolling background
      ObjectManager.animate(lpSecondary); //draw objects
      return TRUE;
    } //ComposeFrame
    
    BOOL ProcessFrame(){ //process a frame of animation
      ComposeFrame(); //compose a frame in secondary surface
      return PageFlip(); //flip video memory surfaces
    } //ProcessFrame
    
    BOOL keyboard_handler(WPARAM keystroke){ //keyboard handler
      BOOL result=FALSE; //return TRUE if game is to end
      switch(keystroke){
        case VK_ESCAPE: result=TRUE; break; //exit game
        case VK_UP: ObjectManager.accelerate(0,-1); break;
        case VK_DOWN: ObjectManager.accelerate(0,1); break;
        case VK_LEFT: ObjectManager.accelerate(-1,0); break;
        case VK_RIGHT: ObjectManager.accelerate(1,0); break;
        case VK_SPACE: ObjectManager.fire_gun(); break;
        default: break;
      }
      return result;
    } //keyboard_handler
    
    //message handler (window procedure)
    long CALLBACK WindowProc(HWND hwnd,UINT message,
                             WPARAM wParam,LPARAM lParam){
      switch(message){
        case WM_ACTIVATEAPP: ActiveApp=wParam; break;
        case WM_CREATE: break;
        case WM_KEYDOWN: //keyboard hit
          if(keyboard_handler(wParam))DestroyWindow(hwnd);
          break;
        case WM_DESTROY: //end of game
          if(lpDirectDrawObject!=NULL){ //if DD object exists
            if(lpSecondary!=NULL) //if secondary surface exists
              lpSecondary->Release(); //release secondary surface
            if(lpPrimary!=NULL) //if primary surface exists
              lpPrimary->Release(); //release primary surface
            if(lpBackground!=NULL) //if background exists
              lpBackground->Release(); //release background
            for(int i=0; i<NUM_SPRITES; i++){ //for each sprite
              if(g_pSprite[i]) //if sprite exists
                g_pSprite[i]->Release(); //release sprite
              delete g_pSprite[i]; //delete sprite
            }
            lpDirectDrawObject->Release(); //release DD object
          }
          ShowCursor(TRUE); //show the mouse cursor
          PostQuitMessage(0); //and exit
          break;
        default: //default window procedure
          return DefWindowProc(hwnd,message,wParam,lParam);
      } //switch(message)
      return 0L;
    } //WindowProc
    
    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,int nCmdShow){
      MSG msg; //current message
      HWND hwnd; //handle to fullscreen window
      hwnd=CreateDefaultWindow("directX demo 6",hInstance);
      if(!hwnd)return FALSE;
      //set up window
      ShowWindow(hwnd,nCmdShow); UpdateWindow(hwnd);
      SetFocus(hwnd); //allow input from keyboard
      ShowCursor(FALSE); //hide the cursor
      //init graphics  
      for(int i=0; i<NUM_SPRITES; i++) //null out sprites
        g_pSprite[i]=NULL;
      BOOL OK=InitDirectDraw(hwnd);//initialize DirectDraw
      if(OK)OK=LoadImages(); //load images from disk
      if(!OK){ //bail out if initialization failed
        DestroyWindow(hwnd); return FALSE;
      }
      //start game timer
      Timer.start();
      //create objects
      CreateObjects();
      //message loop
      while(TRUE)
        if(PeekMessage(&msg,NULL,0,0,PM_NOREMOVE)){
          if(!GetMessage(&msg,NULL,0,0))return msg.wParam;
          TranslateMessage(&msg); DispatchMessage(&msg);
        }
        else if(ActiveApp)ProcessFrame(); else WaitMessage();
    } //WinMain
    Thanks alot!

    Your help is appreciated!

    Batman!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > was wondering why my code wasn't displaying bitmaps properly
    Did it ever do anything properly - like it used to work and now it doesn't?

    Many recently fledged newbies seem to think they can write 100's of lines of code at a sitting without bothering to compile and test as they go. This is of course just another lesson.

    Did you for example make sure everything worked OK before adding more code, or is this your first cut and it doesn't work?

    > BOOL LoadWATERSprite(){ //load bullet
    Many of your comments don't even match the code they're describing, so it's hard to even say whether you even know what should be happening.

    Seriously, there's not a lot we can do since you've omitted all your header files and all the data you load at run time. We can look and make vague suggestions, but you're unlikely to get a concrete "you need to do X" to make it work.

    Try creating a new separate project just to test the idea of loading/displaying a bitmap - nice simple code in one single source file. Then at least when you get stuck, you have a manageable amount of code which you can post to the board in entirety. Who knows, exploring a simple case may reveal the answer to you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    Looks like a cut+paste job.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Displaying fractions simple problem
    By newby01 in forum C Programming
    Replies: 1
    Last Post: 01-19-2008, 02:30 AM
  2. Searching and displaying problem in my program
    By adib in forum C++ Programming
    Replies: 4
    Last Post: 12-31-2007, 05:23 AM
  3. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  4. DX9 Not Displaying Bitmaps
    By Sentral in forum Game Programming
    Replies: 9
    Last Post: 01-31-2006, 05:35 AM
  5. Problem with array in displaying a string
    By Makoy in forum C++ Programming
    Replies: 2
    Last Post: 10-17-2004, 04:45 PM