Wednesday, October 20, 2010

Project 1 - Building the window

We're just starting out this project. While seemingly simple, going so far as creating a window actually requires some setting up. Essentially I have to instruct my developing environment to be aware of the tools I want to use.

I code in c++, basically because it is the first language I learned and the one I've got the most experience with. I am toying around with CLISP, I'll probably write something up about how that goes eventually, but I wouldn't feel comfortable coding with it just yet.

I am writing my code in Code::Blocks, paired with mingw for compiling. They're pretty cool open source projects. I'm kind of a fan of open, accessible software, so I'm doing my best to give back with this project. I'll be using OpenGL for rendering (when I get to it), coupled with the SDL libraries for interfacing with the OS. This should help make the program compatible with most platforms eventually.

Not that I expect to have such a wide audience that this is required, but I think it's a healthy habit to nurture.

So, on to the project itself. Installed the SDL headers and libraries, and my compiler is aware of where they are. Next up, telling the project itself to link with the required libraries while compiling.

And already ran into the first problem. Just #including the SDL.h header is causing issues. Yay. The error I'm getting back is

c:\mingw\bin\..\lib\gcc\mingw32\4.4.0\..\..\..\libmingw32.a(main.o):main.c|| undefined reference to `WinMain@16'|


So, time to do a quick google search.... Well, that wsn't too helpful. Thankfully, I had a tutorial example that helped me along. The problem? I linked the libraries in the wrong order.


When including SDL.h, in mingw, I have to tell the linker to use mingw32, SDLmain, and SDL. In that order. Live and learn. And hopefully, with the blog, remember.

Behold:

#include <SDL/SDL.h>

int main( int argc , char * argv[] )
{
    return 0 ;
}

Well, not very exciting. And a little far from being a window just yet. But it compiles!

Now we can get started. I'm using SDL to interact with the OS, which includes creating a window. If I didn't, I'd be working in a console. And we don't want that. So first we have to start SDL up. For now, I only want to start the video subsystem, so I start off with

SDL_Init( SDL_INIT_VIDEO )

Now we need to actually create the window. This is done with a call to

SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF)

which returns a pointer to a SDL surface with the qualities established in the parameters. SDL surfaces are the 'canvas' we will be drawing on, and the call creates a working window. the first flag tells it to use hardware memory, the second to be double buffered (meaning the image is first drawn in memory, then sent to the screen, instead of drawing directly to the screen). It should help with flickering.

And done. Got a window! The 'program' still does nothing, but I have a starting point. Next up: get OpenGL running by the weekend.

See ya!

No comments:

Post a Comment