Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Rhay

Tutorial: SDL and Windows API

This topic is 5218 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

SDL and Windows API Hello! I'd like to share a piece of code, which shows how to add a Win32 menu bar to an SDL application. I hope somebody finds it useful. I thought of posting this as an article in Sweet Snippets, but I'm an amateur programmer and I'm afraid that it may be buggy. Well, here it is. I assume that you have SDL set up and tested (the basic applications compiles and runs). Let's start with a very basic resource script file with a simple menu that has only one option - File->Quit:
*** res.rc ***
#include "res.h"

IDR_MENU MENUEX
BEGIN
  POPUP "File",IDM_FILE
  BEGIN
    MENUITEM "Quit",IDM_QUIT
  END
END


and res.h with definitions:

*** res.h ***  
#define IDR_MENU 10000
#define IDM_FILE 10001
#define IDM_QUIT 10002
Here goes the main file: *** main.cpp ***
#include "windows.h"       // for all WinAPI stuff

#include "SDL.h"
#include "SDL_syswm.h"     // for platform dependant system messages

#include "res.h"           // resource definitions


SDL_Surface* screen;       // our screen surface


int main(int argc, char *argv[])
{
    // Init SDL video subsystem. Trying to get the window handle before

    // initiating the video gives error, so the first MessageBox() is called

    // without specifying the parent window.

    if(SDL_Init(SDL_INIT_VIDEO)==-1)
    {
        MessageBox(0, SDL_GetError(), "Error", MB_ICONEXCLAMATION | MB_OK);
        return 1;
    }
    // To obtain the window's handle we must pass SDL_SYSWMinfo structure

    // with filled version member to SDL_GetWMInfo function

    // We'll use SDL_VERSION macro from SDL_version.h, which was included

    // by SDL_syswm.h

    // If SDL_GetWMInfo is not implemented, or the version member

    // of the 'info' structure is invalid, it returns 0

    SDL_SysWMinfo mInfo;
    SDL_VERSION(&mInfo.version);
    if(!SDL_GetWMInfo(&mInfo))
    {
        MessageBox(0, "SDL_GetWMInfo Error", "Error", MB_ICONEXCLAMATION | MB_OK);
        return 1;
    }
    // If everything goes OK we have the handle of our window, so we can use it

    // as the parent parameter for MessageBox() calls

    HWND mHandle = mInfo.window;
    MessageBox(mHandle, "Got the handle!", "Message", MB_ICONINFORMATION | MB_OK);
    // Let's now load the menu from resource and set it as active

    HMENU mMenu = LoadMenu(GetModuleHandle(0), MAKEINTRESOURCE(IDR_MENU));
    SetMenu(mHandle, mMenu);
    // The docs say:

    // The system window manager event contains a pointer to system-specific

    // information about unknown window manager events. If you enable this event

    // using SDL_EventState(), it will be generated whenever unhandled events

    // are received from the window manager.

    // Shall we?

    SDL_EventState(SDL_SYSWMEVENT, SDL_ENABLE);
    // Initialization of the main surface

    atexit(SDL_Quit);
    screen = SDL_SetVideoMode(400, 300, 8, SDL_SWSURFACE);
    if(!screen)
    {
        MessageBox(mHandle, SDL_GetError(), "Error", MB_ICONEXCLAMATION | MB_OK);
        return 1;
    }
    // The main program loop

    SDL_Event event;
    bool done = false;
    // Repeat until we're done...

    while(!done)
    {
        while(SDL_PollEvent(&event))
        {
            switch(event.type)
            {
                case SDL_QUIT:
                    done = true;
                    break;
                // Here comes an unknown window manager event

                // event.syswm.msg contains a pointer to a structure:

                // struct SDL_SysWMmsg {

	        //        SDL_version version;

	        //        HWND hwnd;			/* The window for the message */

	        //        UINT msg;			/* The type of message */

	        //        WPARAM wParam;		/* WORD message parameter */

	        //        LPARAM lParam;		/* LONG message parameter */

                // };

                case SDL_SYSWMEVENT:
                    // Let's check it's type

                    switch(event.syswm.msg->msg)
                    {
                    	// User selected a command item from a menu, a control sent

                        // a notification message to its parent window, or an accelerator

                        // keystroke was translated

                        case WM_COMMAND:
                            // Who's job is this?

                            switch(LOWORD(event.syswm.msg->wParam))
                            {
                            	// File->Quit selected, so we're done

                                case IDM_QUIT:
                                    done = true;
                                    break;
                            }
                            break;
                    }
                    break;
                default:
                    break;
            }
        }
    }
    DestroyMenu(mMenu);
    SDL_FreeSurface(screen);
    return 0;
}
And that's all! [edited by - Rhay on June 4, 2004 9:04:02 AM]

Share this post


Link to post
Share on other sites
Advertisement
Doing this of course violates all platform-independence you achieve by using SDL in the first place, but it''s a good little bit of code to show beginners how to enable menus using the Win32 API.


[ CodeDread ]

Share this post


Link to post
Share on other sites
This is actually a really helpful start to use SDL as a graphics wrapper in a windows environment. While achieving platform independence is a great thing, ignoring the benefits of an already existing framework in the case of say a level editor can be very time consuming. I see no problems in using SDL as a graphics wrapper for win32 programming. Using GDI can be cumbersome at times whereas I find SDL (I am relatively new to SDL) to be very straight forward. Not to mention that if you write your game using SDL, adding an editor mode with menus and dialogs wouldn''t seem like such a daunting task to undertake as opposed to writing your own GUI code to mimic win32 menus and buttons that already exist. Granted for the actual game some form of GUI code is necessary, but in a much simpler form than is useful for an editor.

Sorry for the rant. I just looked up and realized I went on about nothing in particular. Thanks to the OP I have a new project to undertake

Share this post


Link to post
Share on other sites
Please someone
All i want to do is blit a bitmap, I tried the sdl but it
doesn''t seem to work on Borland c++ Builder 6. I would need a
tutorial, because all this linking ect. just confuses me.
If it helps- I am a former VB programmer, although I have
I think enought experiance in c++ to do something graphical.

Share this post


Link to post
Share on other sites
I can write a quick example then, it would be something like this...

Make sure you link to sdl.lib, sdlmain.lib and sdl_image.lib



#include <sdl.h>
#include <sdl_image.h>

void blit( SDL_Surface* source, SDL_Surface* target, int xs, int ys, int width, int height, int xt, int yt )
{
SDL_Rect s, t;
s.x = xs;
s.y = ys;
s.w = width;
s.h = height;
t.x = xt;
t.y = yt;
SDL_BlitSurface (source, &s, target, &t );
}

int main( int argc, char* argv[] )
{
SDL_Init ( SDL_INIT_VIDEO ) ;
atexit ( SDL_Quit ) ;
SDL_Surface* screen = SDL_SetVideoMode ( 800 , 600 , 0,SDL_RESIZABLE );
SDL_Surface* picture = IMG_Load("nameofpicture.bmp");
SDL_Event event;
while(1)
{

SDL_FillRect(screen, NULL, 0);
blit( picture, screen, 0,0, picture->w, picture->h, 0, 0 );
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
return 0;
}
}
SDL_Flip( screen );
}

return 0;
}



[edited by - Khaos Dragon on June 9, 2004 5:23:07 PM]

[edited by - Khaos Dragon on June 9, 2004 5:24:25 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!