2D Game programming... where to begin?

Pages: 12
Hi, I have some basic knowledge of c++(I've made a Siri like program and a prank virus(harmless) and some other basic MSDOS programs), but I don't have any experience in graphical programming.
I want to make a game to surprise people... and also to have something for school to show off. So by the way this is my first year of high school and we are going to have programming classes.

Whatever... could you give me some advice to get me started. Resources, tutorials and codes will be highly appreciated.

Thanks in advance.

closed account (Dy7SLyTq)
there are a lot of things you can use
win32 (if your on windows)
sfml + opengl (i dont know actually if you need opengl for just 2d in sfml)
sdl
opengl
Ok I know the names but I need tutorials. Please don't make me google it!
Btw I've heard that sdl is easier. Is it true
closed account (3qX21hU5)
sfml + opengl (i dont know actually if you need opengl for just 2d in sfml)


No you don't need openGL knowledge to use SFML.

Btw I've heard that sdl is easier. Is it true


It alll depends on what you prefer more. Personally I believe SFML is much easier to understand then SDL but then again I prefer a C++ style library over a C style one. It really comes down to what your prefer.

But basically I would say it comes down to two choices when you are just beginning graphics programming in C++ and need a easy library. SFML and SDL. The others will be a bit more advanced.

For tutorials you have these.

SDL - I am a SFML user so I don't really deal with SDL that much but lazyfoo (Google it) has a bunch of tutorials for SDL. Just make sure if you go with SDL to get the new 2.0 version.

SFML - Go to sfml-dev.org they have beginner tutorials there that should get you setup with SFML and learn the very basics of the library. After that you have a couple choices. You can google for SFML tutorials (There is quite a few good ones out there) or if you want a more in-depth learning experience with SFML you can get the new book that just came out on it.


The choice is really up to you. Try them out and choose whatever feels more comfortable to you. Though be sure you know the basics of C++ very well and you should also know the advance topics well also.

Anyways good luck and if you have any other questions feel free to ask.
Thanks Zereo, But I'm still uncertain of how they work which one to get started with. Btw I'll try sfml and ask more questions.

But can any body show me the difference between them. May be a code in both libs that do the same thing and I can see the difference.

EDIT: I have Dev-C++. So do they work with it or only in Code::Blocks and Visual studio
Last edited on
closed account (3qX21hU5)
But I'm still uncertain of how they work which one to get started with


The only way to figure that out is to try them out and look at the tutorials. If you have a good grasp on C++ it shouldn't be that hard.

But can any body show me the difference between them. May be a code in both libs that do the same thing and I can see the difference.


I would recommend just checking out the sample code on both sites to compare them though here is two very basic examples.

SFML - Shows a green circle on a window.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
    sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.draw(shape);
        window.display();
    }

    return 0;
}


SDL - Draws stuff on window.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//Include SDL functions and datatypes
#include "SDL/SDL.h"

int main( int argc, char* args[] )
{
    //The images
    SDL_Surface* hello = NULL;
    SDL_Surface* screen = NULL;
     //Start SDL
    SDL_Init( SDL_INIT_EVERYTHING );

    //Set up screen
    screen = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE );

    //Load image
    hello = SDL_LoadBMP( "hello.bmp" );
    //Apply image to screen
    SDL_BlitSurface( hello, NULL, screen, NULL );

    //Update Screen
    SDL_Flip( screen );

    //Pause
    SDL_Delay( 2000 );
    //Free the loaded image
    SDL_FreeSurface( hello );

    //Quit SDL
    SDL_Quit();

    return 0;
}


EDIT: I have Dev-C++. So do they work with it or only in Code::Blocks and Visual studio


It can work with any compiler and IDE but it will be a bit harder to setup since you will need to recompile the library to your compiler if they don't have a prebuilt binary for your compiler already.

Personally I would recommend just getting codeblocks of Visual Studios but it is up to you.
Yeah I used to work with code blocks but in our school they use Dev-C++ so i preferred to use it.

Can you change the name space to sf(with sfml) so that you won't need to write sf:: each time?

I think SFML is shorter, but sdl is so long(i think it's because of the comments which makes it easier to understand)

Thanks, btw

And... another thing. It may be very soon for this... but how can you handle key presses and move the thing.
closed account (3qX21hU5)
Can you change the name space to sf(with sfml) so that you won't need to write sf:: each time?


Yup its just like with std namespace. You can do using namespace sf; using sf::event;, or just write sf::Event (Which is probably the best way. Also by this I mean put sf:: in front of everything in the sf namespace not just type sf::Event.). Though I wouldn't recommend including the whole sf namespace, but it is up to you.


And... another thing. It may be very soon for this... but how can you handle key presses and move the thing.


Well in SFML you can handle event's with the window.pollEvent loop. Which is this in the code above.

1
2
3
4
5
6
sf::Event event;
while (window.pollEvent(event))
{
    if (event.type == sf::Event::Closed)
        window.close();
}


That is called your event loop which will handle all the events (Like when someone presses a key on the keyboard).

We only have one event in there so far which is sf::Event::Closed which will close the window whenever a close event is generated (Like pressing the X button, or pressing exit hotkeys like alt-f4).

If we wanted to handle when a player presses the W key we could do something like this.

1
2
3
4
5
6
if (event.type == sf::Event::KeyPress)   // Checks if a key was pressed
{
    // If so we figure out which key was pressed
    if (event.key.code == sf::Keyboard::W)
        // Do whatever we want for when W was pressed.
}


We would then define whatever keys we want to watch input for inside of the first if statement and define whatever actions for the individual keys.

More info on it can be found here http://www.sfml-dev.org/tutorials/2.1/window-events.php .

Though one you get the basics down you will generally make a class that deals with handling input.
Last edited on
So it's kinda like flash.

Thanks a lot you helped me so much. Now I can go and start programming basic games. I'll ask more later.

Btw, I'm downloading Code::Block now
I have a problem...

I downloaded and installed Code::Blocks 12.11 and downloaded and extracted GCC 4.7 MinGW (DW2) - 32 bits SFML SDK
So I'm get and error saying "sfml-graphics-d-2.dll does not exist in your computer" or something like that... So I copied the DLLs to system32 folder in drive C. Now i get an error saying "The procedure entry point__gxx_personality_v0 could not be located in the dynamic link library libstdc++-6.dll."
So what is the problem... I did the setup step by step as told in http://sfml-dev.org/tutorials/2.1/start-cb.php
I don't understand what is the problem.

Thanks in advance
closed account (Dy7SLyTq)
i think its that your using the wrong dlls. is your machince 64 bit?
Yeah it's 64. But it supports 32 too.
closed account (3qX21hU5)
i think its that your using the wrong dlls. is your machince 64 bit?


It doesn't matter what type his machine is 32 bit would be fine on a 64 bit machine.


First I would highly recommend you don't put the .dll's in your system32 folder, that will cause you a lot of problems down the road. Instead dump the .dll's into your executable's folder (Wherever your projects .exe file is put the .dll's). Then try and see if that fixes it. If not continue on below because I believe you have name mangling going on. Also don't forget to delete the .dll files you put into system32 (Be careful what you delete. Only delete what you put in there from SFML).

One solution is to search your computer for libstdc++-6.dll and see if there is more then two of them. Correct me if I am wrong but there should only be one on the system which should be from mingw32 (It is in the mingw folder). If there is more then one you might have had another program that install the same .dll and that would be causing a problem (I'm thinking your dev ++ is the problem and is using a very old version of the .dll file)

If you do have more then one you can try including C:\mingw\bin;(Or wherever you installed mingw at) at the very beginning of your system path which might fix it.
Last edited on
closed account (Dy7SLyTq)
It doesn't matter what type his machine is 32 bit would be fine on a 64 bit machine.

there must be a setting then, because when i was doing sdl 1.2 i used x86... hold on i was mixing 32 bit dlls with 64 bit .a files. could that be the problem here?
Another option if you don't want to deal with this mismatching DLL nonsense (and don't want your users to potentially have to deal with it) is to build SFML from the source and link to it statically. That way there is no dll.

Dynamic linking on Windows is more trouble than it's worth for small projects. Especially with C++ libs.
closed account (3qX21hU5)
^ +1

That would be probably the simplest way to deal with it. In case you don't understand about statically linking the .dll from the tutorails here is some simple steps to help.

1) In codeblocks->settings->compiler->linker settings change all your linkings to sfml-xxx-s-d for debug and sfml-xxx-s for release (IE just add a -s to them).

2) In settings->compiler->compiler settings->#defines add in SFML_STATIC

And you should be good to go.
Wowowoww...
Wait a minute...

So I deleted the DLLs from system32 and copied them to the project folder. I'm still getting the second error: "The procedure entry point__gxx_personality_v0 could not be located in the dynamic link library libstdc++-6.dll."

If you do have more then one you can try including C:\mingw\bin;(Or wherever you installed mingw at) at the very beginning of your system path which might fix it.

What should I exactly do? o_O


^ +1

That would be probably the simplest way to deal with it.


What?

EDIT: I searched my computer for libstdc++-6.dll and found only one result ...\MinGW\bin
Last edited on
What?


He was saying he agreed with my suggestion of statically linking, rather than dynamically linking.

Dynamic Linking = there is a separate DLL that your program need to use at runtime. The DLL contains the code in the library you're using

Static Linking = The code for the library is built directly into your exe, so there is no separate DLL.



Static linking means your exe will be slightly bigger, but it also means you don't have to worry about passing around DLLs with your program. You also don't have to worry about the user having the wrong version of the dll... which appears to be your current problem.
Ok I want to do it with the source file. What should I do now? (I downloaded the source codes from the site)
Should I delete the dlls and other files?
Last edited on
Pages: 12