Wanting to build a 2d RPG but kind of lost

Started by
14 comments, last by noobsauce 10 years, 9 months ago

Okay, so I have a pretty basic understanding of C++. This is roughly the information I've covered so far:

Storing Data and getting user input

Performing calculations and displaying output

If Else Statements

Loops

Functions

Arrays

And references.

This is all great and I enjoy programming but I REALLY want to start working with graphics and move towards 2d games. I've started to look into libraries like SDL and some of it looks really weird and then other parts I can understand. Usually after I get past the initial "setting up the SDL screen" tutorial it gets way advanced and I lose it. I really want to start moving into something with graphics because it's starting to feel like the console just isn't doing it for me anymore. With what I've learned, I have been able to program a decent text based RPG.

The only things I haven't gotten to/don't completely understand when I try to do are pointers and classes.

So I guess what i'm asking is... How do I progress towards making a 2d game of some sort? Where can I start or am I going about it wrong? I've tried looking at source code from other people's games or basic SDL game tutorials but it just seems very overwhelming. Can anyone help me out?

Thanks!

Advertisement

I could write what I'm doing. First find out how to paint any picture on screen. Then how to move it. Later make of it class to better handling. Don't really how to do it in SDL. I started from darkGDK which is in c++ and easy to learn. I suggest you to try it ;)

if you will try, and later same guy have more tuts

You sound a lot like me.

Just gotta give it time and learn all of the basics.


Usually after I get past the initial "setting up the SDL screen" tutorial it gets way advanced and I lose it.

Hi. Are you following Lazy Foo's tutorials? If you aren't, give that a try. His are pretty "famous" for being a good starting point. If you are following them already, you should try doing as the tutorial tells you, and follow each tutorial in the correct order. That way, the topics will get increasingly advanced, not suddenly too advanced.


The only things I haven't gotten to/don't completely understand when I try to do are pointers and classes.

You should make sure you understand how to use pointers first, because the SDL library does require you to use pointers for many of its function calls.

Okay, so I have a pretty basic understanding of C++. This is roughly the information I've covered so far:

Storing Data and getting user input

Performing calculations and displaying output

If Else Statements

Loops

Functions

Arrays

And references.

This is all great and I enjoy programming but I REALLY want to start working with graphics and move towards 2d games. I've started to look into libraries like SDL and some of it looks really weird and then other parts I can understand. Usually after I get past the initial "setting up the SDL screen" tutorial it gets way advanced and I lose it. I really want to start moving into something with graphics because it's starting to feel like the console just isn't doing it for me anymore. With what I've learned, I have been able to program a decent text based RPG.

The only things I haven't gotten to/don't completely understand when I try to do are pointers and classes.

So I guess what i'm asking is... How do I progress towards making a 2d game of some sort? Where can I start or am I going about it wrong? I've tried looking at source code from other people's games or basic SDL game tutorials but it just seems very overwhelming. Can anyone help me out?

Thanks!

You do understand that "RPG" is the hardest most difficult genre to make cause RPG = most genres in one.

IMO you don't need to know pointers and classes to make a rpg, but you will need functions.

You should make some set of goals to achieve:
1: I want to learn how to open a window with SDL library.

2: Draw simple shapes

2: Move shapes around the screen.

3: Load texture and apply to shape

4: Animate the sprite.

5: Time step

(You have a good plan to setup the most basic of game)

^^ good luck.

RPG is not the "most difficult genre", it's probably the most scalable one in term of difficulty since you can do a quite basic RPG.

To be honest "pointers and classes" ARE the answer you are looking for, you NEED to know about them to go further. It's quite simple actually. Classes are like a template of every variables who will contain the data representing an entity(object) and such. Instances are the objects themselves instantiated from the class, you can have multiple instance of the same class in memory at the same time. Pointers are just identifiers in form of number telling you where is the instance of each objects you have is so you can access them to use them.

At your point, I would say you should learn to draw shapes and store them to be able to move them independently. Once you manage to do that you are much more advanced.

Other than that, learning how to use an API (like SDL) is important, knowing the language is one thing, but it means nothing without APIs to do things with. Reading the documentation and official guides are always a great help.

Learn basics. Start small. Make games like pong, galaga, asteroids, tetris. Once you can do those... you'll be ready to move on to bigger things.

Patience.

For more on my wargaming title check out my dev blog at http://baelsoubliette.wordpress.com/

The screen is an array of pixels (usually each pixel is an unsigned 32-bit value, with bits 0-7 being red, 8-15 green, 16-23 blue, and 24-31 unused). All you really need SDL for is to provide you with a pointer to such an array that will show up on the actual screen. Then try drawing simple shapes like rectangles:


void DrawRect(unsigned long *screen, int screenWidth, int x, int y, int w, int h, unsigned long color)
{
    for(int j = 0; j < h; j++)
        for(int i = 0; i < w; i++)
            screen[(y + j) * screenWidth + (x + i)] = color;
}


The SDL image loading and blitting functions aren't really necessary, although they might save you some time compared to learning how to load .bmp files yourself, and write your own blitting functions.

After that, you'll need to figure out how to take keyboard input with SDL. Then you can change the position where you draw the rectangle when the arrow keys are pressed, and you have the beginnings of a game smile.png

As Dunge says, understanding pointers and classes will be the next big leap in your programming abilities. I had trouble with pointers at first too, but they're pretty straightforward once you understand understand what memory is. Again, it's more or less one massive array of bytes. Say you have 1GB of RAM in your computer, then memory is like a 1 billion byte array. A memory address is just a number... an index into that array.

All variables are stored somewhere in memory. Different variable types (char, int, float, double) are different sizes, but all pointers are the same size (4 bytes) because they all just store the address of another variable. The pointer type tells the compiler how to interpret the data at that address.

Classes are the natural evolution of structs in C. But first let's see where structs came from smile.png Say you have 4 player characters in your RPG, and each of them has a hit point variable, and max hit points, and attack power. You could do like this:


int hp[4];
int hpMax[4];
int attack[4];


But that can get very messy because those arrays pretty much have to be global variables, so you can pass a player number to a function and the function will be able to access the player's hit points and such, like this


void AdjustPlayerHP(int player, int change)
{
    hp[player] += change;
    if (hp[player] >= hpMax[player])
        hp[player] = hpMax[player];
    else if (hp[player] <= 0)
        SetPlayerDead(player);
}


Much better to do this


struct Player
{
    int hp;
    int hpMax;
    int attack;
};

Player player[4];

void AdjustPlayerHP(Player *player, int change)
{
    player->hp += change;
    if (player->hp >= player->hpMax)
        player->hp = player->hpMax;
    else if (player->hp <= 0)
        SetPlayerDead(player);
}


That way, the player functions don't need to know about any global variables, and can operate on any Player struct, rather than only the ones in a specific global array.
This is what is known as "object oriented programming". Classes are an even more convenient way of implementing this pattern:


class Player
{
public:
    void adjustHP(int change);
    void setDead();

    int hp;
    int hpMax;
    int attack;
};

Player player[4];

void Player::adjustHP(int change)
{
    hp += change;
    if (hp >= hpMax)
        hp = hpMax;
    else if (hp <= 0)
        setDead();
}


Class member functions have an implicit first argument which is the same as the Player *player argument in the struct version of this code. So instead of calling AdjustPlayerHP(&player[2], -10), you'd call player[2].adjustHP(-10), and &player[2] is implicitly passed as the first argument of the function. And as you can see inside the function, you can access member variables like they're locals, rather than having to put player-> before them. And when setDead() is called, the implicit this pointer is passed along to it as well.


All variables are stored somewhere in memory. Different variable types (char, int, float, double) are different sizes, but all pointers are the same size (4 bytes) because they all just store the address of another variable. The pointer type tells the compiler how to interpret the data at that address.

DekuTree64, with a name like that, I'd expect you to use 8 byte pointers.


All variables are stored somewhere in memory. Different variable types (char, int, float, double) are different sizes, but all pointers are the same size (4 bytes) because they all just store the address of another variable. The pointer type tells the compiler how to interpret the data at that address.

DekuTree64, with a name like that, I'd expect you to use 8 byte pointers.

Nah, 640K ought to be enough for anybody laugh.png

This topic is closed to new replies.

Advertisement