Você está na página 1de 2

Franco Cribari Computer Information Systems 5/14/2014

ITO Project Lessons


Lesson 1: Program a Window
What you will need:
Any C++ IDE, I recommend using Codeblocks for this tutorial.
A brain
SDL you can learn about that here:
http://wiki.codeblocks.org/index.php?title=Using_SDL_with_Code::Blocks
It is recommended that you have prior knowledge in C++ or another programming language like
it.
What you will be doing:
The goal is for you to develop a window using C++, SDL. This Window will provide for you a sort of base
to build off of when you follow my other lessons.
Lets Get Started:
1. When you start up Codeblocks you want to click on Create a new project, and then choose the
SDL project template. Name the project title lesson 1, and choose the project directory.

2. If it is your first time using SDL, you will have to configure SDLs location, make sure you match
up all the folder directories correctly, then hit next. If this is not you first time using SDL, ignore
this step and just click next.
3. Choose the Compiler GNU GCC Compiler if it is not already selected, if you are using another
compiler make sure to configure for that instead. Click Finish.
4. Expand the Sources folder, and the others folder. Click main.cpp twice so that it opens up for
editing. Press f9 to compile the sample code, make sure that it can compile. If it does not
compile this could be due to many issues. If you can compile code just fine, ignore step 5.
5. The usual fix for the compiler not working may be that its location is not recognized. To fix this,
go to the settings tab, then click compiler and debugger, then click the Toolchain executables
tab. Click the button that says Auto-detect, and Codeblocks will find the compilers installation
directory. Click OK and try to compile the sample code again. It should be working.
6. Now that you know you can write code, select all of the code and delete it so that you have a
blank page.
Franco Cribari Computer Information Systems 5/14/2014
ITO Project Lessons
7. Write the following code:
#include <SDL.h>
#include <cstdlib>
#include <stdlib.h>
This code tells Codeblocks what it will be working with.
8. Write the following code:
int main(int argc, char** argv){
SDL_Init(SDL_INIT_VIDEO);
}
This code also sets up more parts of the program. The first and third lines are basically the area
in which you will enter all the main code for the program. The 2
nd
line listed is the line that
initializes SDLs video functions. By default this is already initialized, so if you took that code
away it would not affect the program, though it is good practice to keep using this line of code.
9. Enter this code after the SDL Initialize function,
SDL_Surface* screen = SDL_SetVideoMode(640, 480, 32, SDL_DOUBLEBUF |
SDL_DOUBLEBUF);
bool gamerun = true;
SDL_Event event;

while (gamerun){
while (SDL_PollEvent(&event)){
switch (event.type){
case SDL_QUIT:
gamerun = false;
break;
}
}
}
This code calls the surface screen which is equal to the creation of a window. This window is
then told the parameters of 640 x 480 screen resolution, it calls a Boolean, (true or false
statement) an event, and then creates a while loop. There is also a line of code that says
PollEvent which means that rather than continuously running through the code at all times, it
only reads that code when there it needs to, or a case. For instance, I have case SDL_QUIT: and
then break. This code basically says, in the case that someone tries to exit, do this, then stop. In
this case, we are telling the program, which is currently running, to shut down. Once you run
this code you should get a blank window that comes up, and you can utilize the Exit, Minimize,
and Maximize buttons.

Você também pode gostar