Unable to lock\unlock surface

This is a discussion on Unable to lock\unlock surface within the Game Programming forums, part of the General Programming Boards category; Heres my code I got it from windows game programing for dummies and added on to it. there seems to ...

  1. #1
    Unregistered
    Guest

    Unable to lock\unlock surface

    Heres my code I got it from windows game programing for dummies and added on to it.
    there seems to be a problem with locking or unlocking the surfaces (that is it quits) so could any one tell me the problem.
    I am using msvc++6.



    // INCLUDES ///////////////////////////////////////////////
    #define WIN32_LEAN_AND_MEAN

    #include <windows.h> // include important windows stuff
    #include <windowsx.h>
    #include <mmsystem.h>
    #include <iostream.h> // include important C/C++ stuff
    #include <conio.h>
    #include <stdlib.h>
    #include <malloc.h>
    #include <memory.h>
    #include <string.h>
    #include <stdarg.h>
    #include <stdio.h>
    #include <math.h>
    #include <io.h>
    #include <fcntl.h>

    #include <ddraw.h> // directX includes
    // DEFINES ////////////////////////////////////////////////
    // converts pallets to 16bit rgb
    #define _RGB16BIT(r,g,b) ((b%32)+((g%32)<<5)+((r%32)<<10))

    // defines for windows
    #define WINDOW_CLASS_NAME "WINXCLASS" // class name

    #define WINDOW_WIDTH 320 // size of window
    #define WINDOW_HEIGHT 200

    // MACROS /////////////////////////////////////////////////

    // these read the keyboard asynchronously
    #define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
    #define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

    // PROTOTYPES /////////////////////////////////////////////

    int Game_Init(void *parms=NULL);
    int Game_Shutdown(void *parms=NULL);
    int Game_Main(void *parms=NULL);

    // GLOBALS ////////////////////////////////////////////////

    HWND main_window_handle = NULL; // save the window handle
    HINSTANCE main_instance = NULL; // save the instance
    char buffer[80]; // used to print text
    LPDIRECTDRAW lpdd; // pointer to direct draw object
    LPDIRECTDRAWSURFACE lpddsprimary = NULL; // dd primary surface
    LPDIRECTDRAWSURFACE lpddsback = NULL; // dd back surface
    LPDIRECTDRAWPALETTE lpddpal = NULL; // a pointer to the created dd palette
    DDSURFACEDESC ddsd; // a direct draw surface description struct
    DDSCAPS ddscaps; // a direct draw surface capabilities struct
    HRESULT ddrval; // result back from dd calls

    // FUNCTIONS //////////////////////////////////////////////

    LRESULT CALLBACK WindowProc(HWND hwnd,
    UINT msg,
    WPARAM wparam,
    LPARAM lparam)
    {
    // this is the main message handler of the system
    PAINTSTRUCT ps; // used in WM_PAINT
    HDC hdc; // handle to a device context

    // what is the message
    switch(msg)
    {
    case WM_CREATE:
    {
    // do initialization stuff here
    return(0);
    } break;

    case WM_PAINT:
    {
    // start painting
    hdc = BeginPaint(hwnd,&ps);

    // end painting
    EndPaint(hwnd,&ps);
    return(0);
    } break;

    case WM_DESTROY:
    {
    // kill the application
    PostQuitMessage(0);
    return(0);
    } break;

    default:break;

    } // end switch

    // process any messages that we didn't take care of
    return (DefWindowProc(hwnd, msg, wparam, lparam));

    } // end WinProc

    // WINMAIN ////////////////////////////////////////////////

    int WINAPI WinMain( HINSTANCE hinstance,
    HINSTANCE hprevinstance,
    LPSTR lpcmdline,
    int ncmdshow)
    {

    WNDCLASS winclass; // this will hold the class we create
    HWND hwnd; // generic window handle
    MSG msg; // generic message
    HDC hdc; // generic dc
    PAINTSTRUCT ps; // generic paintstruct

    // first fill in the window class stucture
    winclass.style = CS_DBLCLKS | CS_OWNDC |
    CS_HREDRAW | CS_VREDRAW;
    winclass.lpfnWndProc = WindowProc;
    winclass.cbClsExtra = 0;
    winclass.cbWndExtra = 0;
    winclass.hInstance = hinstance;
    winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    winclass.lpszMenuName = NULL;
    winclass.lpszClassName = WINDOW_CLASS_NAME;

    // register the window class
    if (!RegisterClass(&winclass))
    return(0);

    // create the window
    if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
    "WinX Game Console", // title
    WS_OVERLAPPEDWINDOW | WS_VISIBLE,
    0,0, // x,y
    WINDOW_WIDTH, // width
    WINDOW_HEIGHT, // height
    NULL, // handle to parent
    NULL, // handle to menu
    hinstance,// instance
    NULL))) // creation parms
    return(0);

    // save the window handle and instance in a global
    main_window_handle = hwnd;
    main_instance = hinstance;

    // perform all game console specific initialization
    Game_Init();

    // enter main event loop
    while(1)
    {
    if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
    {
    // test if this is a quit
    if (msg.message == WM_QUIT)
    break;

    // translate any accelerator keys
    TranslateMessage(&msg);

    // send the message to the window proc
    DispatchMessage(&msg);
    } // end if

    // main game processing goes here
    Game_Main();

    } // end while

    // shutdown game and release all resources
    Game_Shutdown();

    // return to Windows like this
    return(msg.wParam);

    } // end WinMain

    // WINX GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////

    int Game_Init(void *parms)
    {
    // this function is where you do all the initialization
    // for your game

    //Pointer 2 ddraw obj
    LPDIRECTDRAW lpdd;

    //used to hold the DirectDraw surface description
    DDSURFACEDESC ddsd;

    //Where the interface pointer will be placed when the surface is created
    LPDIRECTDRAWSURFACE lpddsprimary;

    // create object and test for error
    if (DirectDrawCreate(NULL,&lpdd,NULL))
    return(0);

    // set cooperation level to windowed mode normal
    if (lpdd->SetCooperativeLevel(main_window_handle,
    DDSCL_ALLOWMODEX | DDSCL_FULLSCREEN |
    DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT)!=DD_OK)
    return(0);

    // set the display mode
    if (lpdd->SetDisplayMode(800,600,16)!=DD_OK)
    return(0);

    memset(&ddsd,0,sizeof(ddsd));
    ddsd.dwSize = sizeof(ddsd);
    ddsd.dwFlags = DDSD_CAPS;
    ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;

    // create the surface and check for an error
    if (lpdd->CreateSurface(&ddsd,&lpddsprimary,NULL)!=DD_OK)
    return(0);

    // return success
    return(1);
    } // end Game_Init

    ///////////////////////////////////////////////////////////

    int Game_Shutdown(void *parms)
    {
    // this function is where you shutdown your game and
    // release all resources that you allocated

    if (lpddsprimary!=NULL)
    lpddsprimary->Release();
    // release the directdraw object
    if (lpdd!=NULL)
    lpdd->Release();

    // return success
    return(1);
    } // end Game_Shutdown

    ///////////////////////////////////////////////////////////

    int Game_Main(void *parms)
    {
    USHORT *video_buffer;

    // check of user is trying to exit
    if (KEY_DOWN(VK_ESCAPE)){
    PostMessage(main_window_handle, WM_DESTROY,0,0);
    }

    // set up the surface description to lock the surface
    memset(&ddsd,0,sizeof(ddsd));
    ddsd.dwSize = sizeof(ddsd);

    if (KEY_DOWN(VK_SPACE))
    {
    //lock surface
    lpddsprimary->Lock(NULL,&ddsd,
    DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,NULL);
    //get video pointer
    //video_buffer = (USHORT *)ddsd.lpSurface;

    //video_buffer[rand()%800 + (rand()%600*ddsd.lPitch >> 1)]=
    //_RGB16BIT(rand()%256,rand()%256,rand()%256);

    lpddsprimary->Unlock(ddsd.lpSurface);
    }


    // return success
    return(1);
    } // end Game_Main

  2. #2
    Registered User Generator's Avatar
    Join Date
    Aug 2001
    Posts
    238
    PHP Code:
    int main()
    {
            
    cout << "Doesn't anyone use the code tag's?\n";
            return 
    0;


  3. #3
    Unregistered
    Guest
    what are those

  4. #4
    bobish
    Guest
    Can't any one answer a simple question like this it's not exactly anything complex(I was the origional poster)

  5. #5
    Intranasal Heroin User Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,035
    it looks good to me...explain what it looks like when you run it?
    /*Edit*/
    hmmm I see what ya mean, Don't know what to tell ya
    Last edited by Xterria; 10-05-2001 at 06:18 PM.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Well, what error code is returned when you attempt to lock the surface?

  7. #7
    bobish
    Guest
    I don't get any error it just quits on me.

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Be very specific.

    You call the lock function. Does it return? That is, does the code which happens AFTER the attempt to lock ever get executed? You need to know WHERE it is crapping out on you, so use your debugger's breakpoints, or, alternatively, use file I/O to allow you to see how the execution flows. You need to know if it terminates inside the lock code (which is outside of your program) or if it returns to your program prior to death.

    If lock is not returning at all, you're probably passing malformed arguments to it. If it is returning, what value does it return? the lock function will return an int -- what is its value?

  9. #9
    bobish
    Guest
    Okay the debug program has all these errors in the
    ddsd struct it says CXX0030: Error: the expresion cannot be evaluated. I'm thinking that could be the problem what would the cause be an how could I fix it.

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Well, that's something, but what *really* matters are a few things:

    1) Which sections of code execute, and
    2) What the return values of ALL DirectX function calls are.

    I'd suggest having your program open a global output file, and have every function you use dump all kinds of info into it. Basically, it should tell you that it enetered the function, that it left the function, and the return values for all DirectX calls.

    With a text file like this, you can trace the execution. Without knowing where the program is stopping, and without knowing which directx calls are succeeding and which are failing, it's hard to debug ANYTHING.

    Basically, do something like this:

    ofstream * debugStream; // put this outside all functions, to make it global.

    Then, at the very very beginning of execution, initialize it:

    Code:
    int WINAPI WinMain( HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow)
    { 
    
    debugStream = new ofstream("c:\\debugdump.txt");
    debugStream << "Program execution begun" <<endl;
    And, in every function, write some info about entering and exiting, and write all your return codes to the file, too. Then, you know where errors happen. Otherwise, just using what little you've learned about the problem, it really could be anywhere.

    Oh, and for safety, flush the stream after each write -- because if your program crashes, it could lose a write that had been buffered but not written, and it's important to know exactly where executions stops or errors happen.
    Last edited by The V.; 10-06-2001 at 09:36 PM.

  11. #11
    bobish
    Guest
    I get these errors when including:
    debugStream = new ofstream("c:\\debugdump.txt");
    debugStream<<
    "Program execution begun" <<endl;

    also where to I put ofstream * debugStream;

    heres the errors:
    main.cpp(111) : error C2065: 'debugStream' : undeclared identifier

    main.cpp(111) : error C2440: '=' : cannot convert from 'class ofstream *' to 'int'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast

    main.cpp(112) : error C2297: '<<' : illegal, right operand has type 'char [24]'
    Error executing cl.exe.

    does any one know any good tutorials on debuging I
    clearly don't know enough about it.

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Well, those errors sound like you never included this line:

    ofstream * debugStream;

    near the top of the program (put it just after all your #include statements).

  13. #13
    Unregistered
    Guest
    okay now im geting these error:

    error C2296: '<<' : illegal, left operand has type 'class ofstream *'

    error C2297: '<<' : illegal, right operand has type 'char [24]'

    int WINAPI WinMain( HINSTANCE hinstance,
    HINSTANCE hprevinstance,
    LPSTR lpcmdline,
    int ncmdshow)
    {
    debugStream = new ofstream("c:\\debugdump.txt");
    debugStream << "Program execution begun" <<endl;//the errors are occuring here

  14. #14
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Crap, that was a typo of mine.

    Replace:

    debugStream << ...etc.

    with

    *debugStream << ...etc.

  15. #15
    Unregistered
    Guest
    Two things first when ever i put *debugStream << "what ever message" <<endl<<flush; after a function like this if(lpdd->SetCooperativeLevel(main_window_handle,
    DDSCL_ALLOWMODEX | DDSCL_FULLSCREEN |
    DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT)!=DD_OK)

    *debugStream << "what ever message" <<endl<<flush; return(0);

    then the program stops there but Im not getting any errors there any way so it doesn't matter.

    also i found a few little bugs going through the code again first i had this if (DirectDrawCreate(NULL,&lpdd,NULL))
    return(0);
    instead of this if (DirectDrawCreate(NULL,&lpdd,NULL)!=DD_OK)
    return(0);
    but fixing it didn't seem to change any thing.
    second I declared the DDSURFACEDESC twice once in the globals and once in game_init() that could be the problem also if so how could i fix it.

    About the debug
    I cant get it to write any thing that comes after locking the surface or get the return value for locking the surface. Is this the right way to go this:
    if (KEY_DOWN(VK_SPACE))
    {
    //lock surface
    *debugStream << "space pressed" <<endl<<flush; //this worked

    *debugStream << lpddsprimary->Lock(NULL,&ddsd,
    DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,NULL) <<endl<<flush; // this didn't

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to do surface triangulation?
    By adameye in forum C Programming
    Replies: 2
    Last Post: 02-27-2009, 02:15 PM
  2. When I ask SDL for a hardware surface I don't get one
    By scwizard in forum Game Programming
    Replies: 4
    Last Post: 07-24-2008, 04:56 PM
  3. OpenGL: How to make surface shiny
    By ting in forum Game Programming
    Replies: 6
    Last Post: 06-18-2008, 06:09 PM
  4. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 11:00 PM
  5. Please help with my game
    By jjj93421 in forum Game Programming
    Replies: 10
    Last Post: 04-13-2004, 12:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21