Hello World! This is my first program
Now that I have a working environment it is actually time to start writing some code (It's about time as this is the 5th installment).

As I am only a beginner I don't want to tell you how to develop your own programs as I'm not in the position to tell you if this is the right or wrong way of programming. However the first program did highlight some potential issues you may also encounter and is a good method of testing your environment.

Firstly I should give credit to Brian Kernighan who created the first ever hello world program, that has seen many variations and is widely used in many books and tutorials today.

The following code is my variation of the "hello world" program that will test the SDL Libraries are correctly configured in Visual C++ Express 2010.

Hello World! C++ Code


// My First Steps : C++ Game Development - Hello World! Example
// Author         : Matt Wakeling
// Date Created   : 17th May 2010

// Declare Libraries.
#include <cstdio>
#include <sdl.h>

//Declare SurfaceDetails Function.
void SDL_SurfaceDetails(char * strName, SDL_Surface *objSurface);

// Define main function, allows arguments to be passed.
int main(int argc, char* argv[])
{
    //Initialise SDL Library and Video Subsystem.
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        fprintf(stderr, "Unable to initialise SDL Library: %s\n", SDL_GetError());
        exit(1);
    }
    
    //Set Window Title.
    SDL_WM_SetCaption("Hello World!", "Hello World");

    //Define SDL_Surface object to represent program window.
    SDL_Surface* objScreen = SDL_SetVideoMode(624, 117, 0, 0);
    SDL_SurfaceDetails("Screen Object", objScreen);
    
    //Define SDL_Surface object for the HelloWorld! Bitmap.
    SDL_Surface* objHelloWorld_Bitmap = SDL_LoadBMP("HelloWorld.bmp");
    SDL_SurfaceDetails("HelloWorld! Bitmap", objHelloWorld_Bitmap);
    

    //Define SDL_Surface object to represent program buffer.
    SDL_Surface* objBuffer = SDL_DisplayFormat(objHelloWorld_Bitmap);
    SDL_SurfaceDetails("Buffer Object", objBuffer);

    //Free Allocated Memory for HelloWorld! Bitmap (as it has now no longer required).
    SDL_FreeSurface(objHelloWorld_Bitmap);

    SDL_Event objSDLEvent;
    bool bolQuit = false;

    //Main Program Loop.
    while(!bolQuit)
    {
        //check the message queue for an event
        if (SDL_PollEvent(&objSDLEvent))
        {
            //Check if Program Window has been closed.
            if (objSDLEvent.type == SDL_QUIT)
            {
                bolQuit = true;
            }
            
            //Check if [ESC] Key has been pressed.
            if (objSDLEvent.type == SDL_KEYDOWN && objSDLEvent.key.keysym.sym == SDLK_ESCAPE)
            {
                bolQuit = true;
            }
        }

        //Blit the buffer object to the main screen.
        SDL_BlitSurface(objBuffer, NULL, objScreen, NULL);

        //Update the entire main screen.
        SDL_UpdateRect(objScreen, 0, 0, 0, 0);
    }

    //Free Allocated Memory for Buffer object (as it has now no longer required).
    SDL_FreeSurface(objBuffer);

    //Quit and perform cleanup of SDL.
    SDL_Quit();

    //Exit Program and Return no errors code (0).
    return 0;
}


// Define Surface Details Function for reference.
void SDL_SurfaceDetails(char * strName, SDL_Surface *objSurface)
{
    printf("Surface Object (%s): width:%d height:%d bits_per_pixel:%d\n", strName, objSurface->w, objSurface->h, objSurface->format->BitsPerPixel);
}

Before you execute/debug the code you will also need a copy of the HelloWorld! bitmap, this image was generated using the online logo design site (cooltext). This image should be saved as a bitmap file (*.bmp) in your project directory.


Once compliled and exectuted, the following program should be displayed....


Potential Issues found during compilation and execution
During development and testing of the program the following issues were found and fixed...


"LINK : fatal error LNK1561: entry point must be defined"
This issue was caused by not selecting Win32 Console Application as the empty project.

"Unhandled exception at 0x01091d11 in Project1.exe: 0xC0000005: Access violation reading location 0x00000004."
This issue was caused by two seperate incidents.
  1. The "HelloWorld.bmp" bitmap file was not in the project directory, during debugging/execution. The file was added to the correct directory.
  2. The "HelloWorld.bmp" was actually a JPEG and not a bitmap. File re-saved as bitmap.