NEStix: Programming (C++/SDL)

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Post by Falco Girgis »

Oh lord, when the hell did we start doing this:

Code: Select all

int Function()
{
    /*this method is an eminating monstrosity*/
}
Well, I refuse to conform to your distasteful coding method! :mrgreen:
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Post by Falco Girgis »

Aside from your french braces being so monstrous, I commend you. That was nice.

I'm going to port your Blood Generator now...
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Post by JS Lemming »

Actually, when I first coded it, I used the "Good" style. But when I looked at my finished code, it looked cramped for some reason... By placing the brace after the command, it lines it up with the ending brace. That way, if I don't know what a certain brace closes because the code is too long, i can just count the tabs, scroll up and see which command started at that many tabs.

Also, who here is set up to compile such SDL programs besides Falco and me? We can help you set up if you need it.
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Post by Falco Girgis »

Yeah, okay. I know what you mean. My theory is that this:

Code: Select all

int Function()
{
    //monstrosity
}
generally looks much better when you've got LOTS of notes.

This, is better for everything else:

Code: Select all

int Function() {
    //utter respectability!
}
I implore you to use the latter method with NEStix. Yeah, who else can compile this awesomeness?
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Post by MarauderIIC »

Mmm, that looks so much easier to set up than OpenGL. 'Cause you have to set up a window to draw in w/ OpenGL. Which means using Windows specific functions. Just 'cause OpenGL doesn't handle that stuff.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Don Pwnious
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 833
Joined: Tue Jun 15, 2004 5:32 pm
Location: on the streets wit my j23
Contact:

Post by Don Pwnious »

umm.. mine is not working properly
its says that there is no host application??
1/8th time- 14secs
1/8th speed - 110mph
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Post by JS Lemming »

Heh, I got the EASIER key input stuff to work. Now you can call the keydown or keyhit i made anywhere in your program.

Controls:
Arrow Keys: move box
Space Bar: Flashes a blue background
Escape: Quits

Code: Select all

//demo use of easier key input by JS Lemming

//requires static linkage to:  
//sdl.lib, sdlmain.lib

//requires dynamic linkage to: 
//sdl.dll

//include SDL stuff
#include "sdl.h"

//include ability to exit program
#include <stdlib.h>


//These are the T/F flags that will be used to determine if a
//key is being held down by the user. If true, the key is down... false its not.
bool f_Up;
bool f_Down;
bool f_Left;
bool f_Right;
bool f_Space;
bool f_Escape;

//Function KeyDown(KeyFlag) - checks whether the arg passed to it is true,
//thus returning true. Why have this function at all? For consistancy sakes
//of the KeyHit function.
bool KeyDown(bool KeyFlag) {
    return KeyFlag;
}

//Function KeyHit(KeyFlag) - checks whether the arg passed to it is true.
//Then setting the flag to false, and returning true.
bool KeyHit(bool& KeyFlag) {
    if(KeyFlag == true)
    {
        KeyFlag = false;
        return true;
    }
    return false;
}

//Box class
class Box
{
    public:
        int x, y;
};

//screen dimensions
const int SCREEN_WIDTH=640;
const int SCREEN_HEIGHT=480;

//display surface
SDL_Surface* g_pDisplaySurface;

//event structure
SDL_Event g_Event;

//rectangle
SDL_Rect g_Rect;

//color components
Uint8 g_Red, g_Green, g_Blue;

//color value
Uint32 g_Color;


//main function
int main(int argc, char* argv[])
{
	//initialize SDL
	if (SDL_Init(SDL_INIT_VIDEO)==-1)
	{
		//error initializing SDL

		//report the error
		fprintf(stderr,"Could not initialize SDL!\n");

		//end the program
		exit(1);
	}
	else
	{
		//SDL initialized

		//report success
		fprintf(stdout,"SDL initialized properly!\n");

		//set up to uninitialize SDL at exit
		atexit(SDL_Quit);
	}

    //hide mouse
    SDL_ShowCursor(SDL_DISABLE);

	//create windowed environment
	g_pDisplaySurface = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,24,SDL_ANYFORMAT|SDL_FULLSCREEN);

	//error check
	if (g_pDisplaySurface == NULL)
	{
		//report error
		fprintf(stderr,"Could not set up display surface!\n");

		//exit the program
		exit(1);
	}

    //Create instance of class box
    Box box1;
    box1.x = 140;
    box1.y = 130;


	//main game loop
    while(1)
	{
        //look for an event
        if(SDL_PollEvent( &g_Event ) == true)
        {
            //if user closes out of window, exit loop
            if(g_Event.type==SDL_QUIT)
                break;

            //if ESC key
            if(g_Event.key.keysym.sym == SDLK_ESCAPE )
            {
                // if ESC was pressed, quit the program.
                SDL_Event quit;
                quit.type = SDL_QUIT;
                SDL_PushEvent( &quit );
            }

            //check key input
            switch( g_Event.type )
            {
                //if a key is pressed
                case SDL_KEYDOWN:
                    switch( g_Event.key.keysym.sym )
                    {
                        case SDLK_UP:
                            f_Up = true;
                            break;
                        case SDLK_DOWN:
                            f_Down = true;
                            break;
                        case SDLK_LEFT:
                            f_Left = true;
                            break;
                        case SDLK_RIGHT:
                            f_Right = true;
                            break;
                        case SDLK_SPACE:
                            f_Space = true;
                            break;
                        case SDLK_ESCAPE:
                            f_Escape = true;
                            break;
                    }
                    break;

                //a key is released
                case SDL_KEYUP:
                    switch( g_Event.key.keysym.sym )
                    {
                        case SDLK_UP:
                            f_Up = false;
                            break;
                        case SDLK_DOWN:
                            f_Down = false;
                            break;
                        case SDLK_LEFT:
                            f_Left = false;
                            break;
                        case SDLK_RIGHT:
                            f_Right = false;
                            break;
                        case SDLK_SPACE:
                            f_Space = false;
                            break;
                        case SDLK_ESCAPE:
                            f_Escape = false;
                            break;
                    }
                    break;

                default:
                    break;
            }
        }


        //clear the screen with blue if keyhit space bar, else black
        if( KeyHit(f_Space) ) {
            SDL_FillRect(g_pDisplaySurface, NULL, SDL_MapRGB( g_pDisplaySurface->format, 0, 0, 255 ));
        } else {
            SDL_FillRect(g_pDisplaySurface, NULL, SDL_MapRGB( g_pDisplaySurface->format, 0, 0, 0 ));
        }

        //update the box via user input
        if( KeyDown(f_Up) )
            box1.y = box1.y - 2;
        if( KeyDown(f_Down) )
            box1.y = box1.y + 2;
        if( KeyDown(f_Left) )
            box1.x = box1.x - 2;
        if( KeyDown(f_Right) )
            box1.x = box1.x + 2;


        //create the rectangle
        g_Rect.x = box1.x;
        g_Rect.y = box1.y;
        g_Rect.w = 60;
        g_Rect.h = 60;

        //set the color
        g_Red=0;
        g_Green=200;
        g_Blue=0;

        g_Color=SDL_MapRGB(g_pDisplaySurface->format,g_Red,g_Green,g_Blue);

        //fill the rectangle
        SDL_FillRect(g_pDisplaySurface,&g_Rect,g_Color);

        //update the screen
        SDL_UpdateRect(g_pDisplaySurface,0,0,0,0);
	}

	//normal termination
	fprintf(stdout,"Terminating normally.\n");

	//return to OS
	return(0);
}
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Post by MarauderIIC »

How about bool keys[256] ? Then you can access characters by their ASCII codes. And make the code a lot more general.

Code: Select all

bool keys[256];
bool oldkeys[256];
#define RIGHT_ARROW (whatever the code is. too lazy atm)
#define LEFT_ARROW (whatever the code is. too lazy atm)
#define UP_ARROW ditto
#define DOWN_ARROW ditto


// ... 
case SDL_KEYDOWN:
    keys[g_Event.key.keysym.sym] = true;
    break;
case SDL_KEYUP:
    keys[g_Event.key.keysym.sym] = false;
    oldkeys[g_Event.key.keysym.sym] = false;
    break;
// ....

/* ... */
if (keys['a'] && !oldkeys['a']) {
    // do something (initial press)
    oldkeys['a'] = keys['a']; //or oldkeys['a'] = true
}

if (keys['b']) {
    // do something (Can be held)
}

//jump, but only if they have jumped less than twice
//and only on the initial keypress (holding will not make you doublejump
//immediately)

if (keys[UP_ARROW] && !oldkeys[UP_ARROW] && player->jumpCount() < 2)
{
    // jump
    player->jumpCountAdd(1);
}
else if (keys[DOWN_ARROW] && !player->isJumping())
{
    // crouch
}
Last edited by MarauderIIC on Mon Oct 04, 2004 11:03 pm, edited 1 time in total.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Guest

Post by Guest »

. . . . . . :evil:
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Post by JS Lemming »

Arce wrote:. . . . . . :evil:
Problems?
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Post by Falco Girgis »

He's either bloated, constipated, or he's come to the painful realization that C/++ is PWNing blitz.
User avatar
Don Pwnious
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 833
Joined: Tue Jun 15, 2004 5:32 pm
Location: on the streets wit my j23
Contact:

Post by Don Pwnious »

Super Sonic wrote:He's either bloated, constipated, or he's come to the painful realization that C/++ is PWNing blitz.
he he he
:guffaw:
1/8th time- 14secs
1/8th speed - 110mph
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Post by JS Lemming »

Thanks to my Data Structures for Game Programmers book. I can know use linked lists with relative easy. Download the nice linked lists header file here. http://www.thechaosrift.com/pc/Temp/DLinkedList.h
(be sure to use "Save As")

Using that mug, i came up with a simple bullet like demo. To demonstrate iterateing through a list and updating the items and such. You need the "DLinkedList.h" file above in the same folder to compile.

Controlls:
Up\Down = move box
Space = shoot
Right = Super Rapid Monkey shoot
Esc = exit

Code: Select all

//demo use of linked lists (shooting) by JS Lemming

//requires static linkage to:  
//sdl.lib, sdlmain.lib

//requires dynamic linkage to: 
//sdl.dll

//include SDL stuff
#include "sdl.h"

//include ability to exit program
#include <stdlib.h>

//include Double Linked Lists stuff
#include "DLinkedList.h"


//These are the T/F flags that will be used to determine if a
//key is being held down by the user. If true, the key is down... false its not.
bool f_Up;
bool f_Down;
bool f_Left;
bool f_Right;
bool f_Space;
bool f_Escape;

//Function KeyDown(KeyFlag) - checks whether the arg passed to it is true,
//thus returning true. Why have this function at all? For consistancy sakes
//of the KeyHit function.
bool KeyDown(bool KeyFlag) {
    return KeyFlag;
}

//Function KeyHit(KeyFlag) - checks whether the arg passed to it is true.
//Then setting the flag to false, and returning true.
bool KeyHit(bool& KeyFlag) {
    if(KeyFlag == true)
    {
        KeyFlag = false;
        return true;
    }
    return false;
}

//Bullet class
class Bullet
{
    public:
        int x, y;
        int xvel;
};

//Player class
class Player
{
    public:
        int y;

        DLinkedList<Bullet> m_bullets;

};

//Create instance of class player
Player dude;

//create iterator used to access bullets
DLinkedList<Bullet> g_itr;

//screen dimensions
const int SCREEN_WIDTH=640;
const int SCREEN_HEIGHT=480;

//display surface
SDL_Surface* g_pDisplaySurface;

//event structure
SDL_Event g_Event;

//rectangle
SDL_Rect g_Rect;

//color components
Uint8 g_Red, g_Green, g_Blue;

//color value
Uint32 g_Color;




//Function CreateBullet(x,y,xvel) - adds a bullet to the list o'bullets
void CreateBullet(int x, int y, int xvel)
{
    //create the bullet
    Bullet bullet;
    bullet.x = x;
    bullet.y = y;
    bullet.xvel = xvel;

    //add it to the list
    dude.m_bullets.Append( bullet );
}

//Function UpdateBullet() - iterates through all bullets and
//adjusts and draws them
void UpdateBullet()
{
    DListIterator<Bullet> itr = dude.m_bullets.GetIterator();
    for( itr.Start(); itr.Valid(); itr.Forth() )
    {
        itr.Item().x = itr.Item().x + itr.Item().xvel;

        //make it bounce back if it hit the right wall
        if(itr.Item().x > 640) {
            itr.Item().x = 640;
            itr.Item().xvel = -itr.Item().xvel;
        }

        //create the rectangle
        g_Rect.x = itr.Item().x;
        g_Rect.y = itr.Item().y;
        g_Rect.w = 5;
        g_Rect.h = 2;

        //set the color
        g_Red = 255;
        g_Green = 0;
        g_Blue = 0;

        g_Color=SDL_MapRGB(g_pDisplaySurface->format,g_Red,g_Green,g_Blue);

        //fill the rectangle
        SDL_FillRect(g_pDisplaySurface,&g_Rect,g_Color);

        //delete it if it goes to far left
        if(itr.Item().x < 10) {
            dude.m_bullets.Remove( itr );
        }

    }

}





//main function
int main(int argc, char* argv[])
{
	//initialize SDL
	if (SDL_Init(SDL_INIT_VIDEO)==-1)
	{
		//error initializing SDL

		//report the error
		fprintf(stderr,"Could not initialize SDL!\n");

		//end the program
		exit(1);
	}
	else
	{
		//SDL initialized

		//report success
		fprintf(stdout,"SDL initialized properly!\n");

		//set up to uninitialize SDL at exit
		atexit(SDL_Quit);
	}

    //hide mouse
    SDL_ShowCursor(SDL_DISABLE);

	//create windowed environment
	g_pDisplaySurface = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,16,SDL_ANYFORMAT|SDL_FULLSCREEN);

	//error check
	if (g_pDisplaySurface == NULL)
	{
		//report error
		fprintf(stderr,"Could not set up display surface!\n");

		//exit the program
		exit(1);
	}

    //create init bullet
    CreateBullet(0, 0, 0);

    //set some stuff
   // g_itr = dude.m_bullets.GetIterator();
    dude.y = 230;

	//main game loop
    while(1)
	{
        //look for an event
        if(SDL_PollEvent( &g_Event ) == true)
        {
            //if user closes out of window, exit loop
            if(g_Event.type==SDL_QUIT)
                break;

            //if ESC key
            if(g_Event.key.keysym.sym == SDLK_ESCAPE )
            {
                // if ESC was pressed, quit the program.
                SDL_Event quit;
                quit.type = SDL_QUIT;
                SDL_PushEvent( &quit );
            }

            //check key input
            switch( g_Event.type )
            {
                //if a key is pressed
                case SDL_KEYDOWN:
                    switch( g_Event.key.keysym.sym )
                    {
                        case SDLK_UP:
                            f_Up = true;
                            break;
                        case SDLK_DOWN:
                            f_Down = true;
                            break;
                        case SDLK_LEFT:
                            f_Left = true;
                            break;
                        case SDLK_RIGHT:
                            f_Right = true;
                            break;
                        case SDLK_SPACE:
                            f_Space = true;
                            break;
                        case SDLK_ESCAPE:
                            f_Escape = true;
                            break;
                    }
                    break;

                //a key is released
                case SDL_KEYUP:
                    switch( g_Event.key.keysym.sym )
                    {
                        case SDLK_UP:
                            f_Up = false;
                            break;
                        case SDLK_DOWN:
                            f_Down = false;
                            break;
                        case SDLK_LEFT:
                            f_Left = false;
                            break;
                        case SDLK_RIGHT:
                            f_Right = false;
                            break;
                        case SDLK_SPACE:
                            f_Space = false;
                            break;
                        case SDLK_ESCAPE:
                            f_Escape = false;
                            break;
                    }
                    break;

                default:
                    break;
            }
        }


        //clear the screen with blue if keyhit space bar, else black
        SDL_FillRect(g_pDisplaySurface, NULL, SDL_MapRGB( g_pDisplaySurface->format, 0, 0, 0 ));

        //        if( KeyHit(f_Space) ) {

        //update the box via user input
        if( KeyDown(f_Up) )
            dude.y = dude.y - 2;
        if( KeyDown(f_Down) )
            dude.y = dude.y + 2;

        if( KeyDown(f_Right) )
            CreateBullet(100, dude.y+5, 4);

        if( KeyHit(f_Space) )
            CreateBullet(100, dude.y+5, 4);


        UpdateBullet();

        //create the rectangle
        g_Rect.x = 40;
        g_Rect.y = dude.y;
        g_Rect.w = 60;
        g_Rect.h = 10;

        //set the color
        g_Red = 0;
        g_Green = 200;
        g_Blue = 0;

        g_Color=SDL_MapRGB(g_pDisplaySurface->format,g_Red,g_Green,g_Blue);

        //fill the rectangle
        SDL_FillRect(g_pDisplaySurface,&g_Rect,g_Color);

        //update the screen
        SDL_UpdateRect(g_pDisplaySurface,0,0,0,0);
	}

	//normal termination
	fprintf(stdout,"Terminating normally.\n");

	//return to OS
	return(0);
}
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Post by MarauderIIC »

VETORS! VEEECTOOORRSS are standardized linked lists. Use!!1
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Post by Falco Girgis »

After school, I'm going to bust out my 2 C++ books. I'm going to read every page with the word "vector" on it. Then I'll be able to say "vectorsss!" with Mar. Or at least nod in agreement!
Post Reply