forums.evilmana.com
Welcome, Guest. Please login or register.

Login with username, password and session length
July 29, 2010, 01:37:45 PM
.
News: 08/07/2009 - Welcome to the revived and revamped evilmana forums. Take a look around  for new features such as:New Theme, New Reputation System, and Arcade!
Coming Soon: An all new evilmana.com main site!
Pages: [1]   Go Down
0 Members and 1 Guest are viewing this topic. Topic Tools  
Read February 18, 2010, 01:41:53 PM #0
emcp

Layout Of Game

This is probably the millionenth time ive sat down to learn c++, over the course of 6 years,
ive continuously made improvements

And over the last 6 months, i truely learnt some of the more advance areas of game programming, the theory atleast and pseudo code

The Thing no tutorial covers is the basic layout of a Game, and why it is that way for example

In lua i would store the player attributes in an array
and simply add or subtract from the appropriate values, in he main loop once a button was pressed

however in C++ it is totally different, or atleast the sample code is for example

a class would be created
Code:
class Player
{
    int x,y,z;

    public:

    void SetLoc(int a,int b,int c){
        x=a;
        y=b;
        z=c;
    }

    void Move(char a){
        if (a=='F') z--;
        else if (a=='B') z++;

        else if (a=='L') x--;
        else if (a=='R') x++;

        else if (a=='U') y++;
        else if (a=='D') y--;
    }
};

now the sample code normally provides something like SetLoc, but why not stick x,y,x as public and simply put this before the main loop

Player().x=1.f


how should i handle movement at the moment, ive got an event listener which listens for a key in sdl,

in my main loop i have this
Code:
       if (Ctrl::Up) Player().Move('F');
        if (Ctrl::Down) Player().Move('B');
        if (Ctrl::Left) Player().Move('L');
        if (Ctrl::Right) Player().Move('R');

But why not just have Player().x++

Thanks
Also some tips on scope would be helpful

heres my full code
Theres redundant pieces here and there
Code:
#include <windows.h>

#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>

#include <stdio.h>
#include <math.h>

#define PI  3.14159265
float ToRad = PI/180;
float ToDeg = 180/PI;

namespace Ctrl
{
    bool Up=false;
    bool Down=false;
    bool Left=false;
    bool Right=false;

    bool A=false;
    bool S=false;

    bool Space=false;
    bool OldSpace=false;
}

// Handle events
bool Events()
{
    SDL_Event event;

    while (SDL_PollEvent(&event))
    {
        switch (event.type)
        {
            case SDL_KEYDOWN:
            {
                if (event.key.keysym.sym == SDLK_ESCAPE) return false;

                if (event.key.keysym.sym == SDLK_UP) Ctrl::Up=true;
                else if (event.key.keysym.sym == SDLK_DOWN) Ctrl::Down=true;
                if (event.key.keysym.sym == SDLK_LEFT) Ctrl::Left=true;
                else if (event.key.keysym.sym == SDLK_RIGHT) Ctrl::Right=true;

                if (event.key.keysym.sym == SDLK_a) Ctrl::A=true;
                else if (event.key.keysym.sym == SDLK_s) Ctrl::S=true;

                if (event.key.keysym.sym == SDLK_SPACE) Ctrl::Space=true;

                break;
            }
            case SDL_KEYUP:
            {
                if (event.key.keysym.sym == SDLK_UP) Ctrl::Up=false;
                else if (event.key.keysym.sym == SDLK_DOWN) Ctrl::Down=false;
                if (event.key.keysym.sym == SDLK_LEFT) Ctrl::Left=false;
                else if (event.key.keysym.sym == SDLK_RIGHT) Ctrl::Right=false;

                if (event.key.keysym.sym == SDLK_a) Ctrl::A=false;
                else if (event.key.keysym.sym == SDLK_s) Ctrl::S=false;

                if (event.key.keysym.sym == SDLK_SPACE) Ctrl::Space=false;

                break;
            }
            case SDL_QUIT:
                return false;
        }
    }
return true;
}

class Player
{
    public:

    int x,y,z;

    void SetLoc(int a,int b,int c){
        x=a;
        y=b;
        z=c;
    }

    int RetLoc(){
        return x,y,z;
    }

    void Move(char a){
        if (a=='F') z--;
        else if (a=='B') z++;

        else if (a=='L') x--;
        else if (a=='R') x++;

        else if (a=='U') y++;
        else if (a=='D') y--;
    }
};

void InitGraphics(){
    const int width = 800, height = 480;

    // Initialize SDL
    if (SDL_Init(SDL_INIT_VIDEO) < 0) exit(1);

    // Window Settings
    SDL_WM_SetCaption("SDL/OpenGL Sample", "SDL/OpenGL Sample");

    // Get Video Info
    const SDL_VideoInfo *video = SDL_GetVideoInfo();
    if (!video) { SDL_Quit(); exit(2); }

    // Create Video Surface
    SDL_Surface *screen = SDL_SetVideoMode(width, height, video->vfmt->BitsPerPixel, SDL_OPENGL);
    if (!screen) { SDL_Quit(); exit(3); }




    // Size OpenGL to Video Surface
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, (float)width / (float)height, 1.0, 1000.0);
    glMatrixMode(GL_MODELVIEW);

    // Set Pixel Format
    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

    // OpenGL Render Settings
    glClearColor(0, 0, 0, 1);
    glClearDepth(1.0);
    glEnable(GL_DEPTH_TEST);
}

void Draw(){
    // Set location in front of camera
    glLoadIdentity();
    glTranslated(Player().x,Player().y,Player().z);
    glRotated(0,0,0,1);

    // Draw a outline polygon
    glBegin(GL_LINE_LOOP);
        glColor3d(1, 0, 0);
        glVertex3d(0, 20, 0);
        glVertex3d(20, -20, 0);
        glVertex3d(0, 0, 0);
        glVertex3d(-20, -20, 0);
    glEnd();
}


// Program entry point
//int main(int argc, char **argv)
int WINAPI WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow
)
{

    InitGraphics();
    Player().SetLoc(-1.f,-1.f,-500.f);

    while (Events())
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        if (Ctrl::Up) Player().Move('F');
        if (Ctrl::Down) Player().Move('B');
        if (Ctrl::Left) Player().Move('L');
        if (Ctrl::Right) Player().Move('R');

        Draw();

        // Show the frame
        SDL_GL_SwapBuffers();
    }
    // Clean up
    SDL_Quit();
    return 0;
}

It doesnt seem to be drawing anything because Draw is not playing ball, if i empty the code out of the function Draw and just paste it in, it works fine
« Last Edit: February 18, 2010, 01:46:10 PM by emcp »

Offline  
Read February 18, 2010, 10:18:10 PM #1
kartracer12

Re: Layout Of Game

First, the class...
There is nothing stopping you from making x,y,z public. The tutorial was probably just trying to make a point, that you can use a function to set these values. A lot of what c++ does is safeguard things. there is no point in making an integer constant by adding the keyword const, it just makes sure that it doesn't get changed (ie. makes sure that you write "==" instead of "="), but if you write the code correct, you should be able to take out that keyword const and it would run the same. Similarly, making values private instead of public ensures the same thing.

The second thing...
I don't know too much about SDL or openGl (i believe SDL is a openGL "wrapper"), but it seems odd to me that the drawing function has no parameters involving a handle to the window. Something to look into. It seems weird that putting the function code straight into the main loop would make a difference if that was the issue, but who knows?
Offline  
Read February 19, 2010, 04:22:40 AM #2
yaustar

Re: Layout Of Game

Code:
    int RetLoc(){
        return x,y,z;
    }
Doesn't work. It either just returns x or z (I can't remember). C++ doesn't have tuples. You would want a struct/class that handles positional data.

Since you are using C++, use the C++ versions of the C library headers which changes defines to consts, marcos to inline functions, etc. Basically it C++ ify's it.
stdio.h -> cstdio
math.h -> cmath

Use named consts over defines for constants. Easier to debug, you get type safety and the compiler optimises this out to be inline regardless so performance is exactly the same as using a define. Also, cmath/math.h already has a PI constant IIRC.

Code:
    void Move(char a){
        if (a=='F') z--;
        else if (a=='B') z++;

        else if (a=='L') x--;
        else if (a=='R') x++;

        else if (a=='U') y++;
        else if (a=='D') y--;
    }
Use a switch statement instead.

Code:
The Thing no tutorial covers is the basic layout of a Game, and why it is that way for example
Read Game Coding Complete and C++ for Game Programmers.

Quote
But why not just have Player().x++
Code reuse/duplication. Because if you need to change the way that the player moves, you only have to change it in one place if you had it as a function rather then in many places if you did it the way above. Read Code Complete.

Code:
namespace Ctrl
{
    bool Up=false;
    bool Down=false;
    bool Left=false;
    bool Right=false;

    bool A=false;
    bool S=false;

    bool Space=false;
    bool OldSpace=false;
}
Why are these global?
« Last Edit: February 19, 2010, 04:32:12 AM by yaustar »

Offline  
Read February 19, 2010, 06:25:17 PM #3
_df_

Re: Layout Of Game

I would either code move() to take dx,dy,dz and wrap it all in a single function, eg : left = move(-1,0,0); etc. or just code MoveUp(), MoveDown(), etc separate funcs. This passing constant stuff in, is imo a poor way to do it.
Offline  
Read February 23, 2010, 10:23:40 AM #4
emcp

Re: Layout Of Game

I would either code move() to take dx,dy,dz and wrap it all in a single function, eg : left = move(-1,0,0); etc. or just code MoveUp(), MoveDown(), etc separate funcs. This passing constant stuff in, is imo a poor way to do it.

i actually did that seconds after posting the above code Wink
thanks to all, didnt have anything to contribute to the discussion hence no post-back

but i can see me needing your help sometime soon (to tidy up a few things)


Offline  
Pages: [1]   Go Up
Jump to:  

Theme Update by Runic Warrior Originally created by m3talc0re