Code: Select all
int Function()
{
/*this method is an eminating monstrosity*/
}
Moderator: Coders of Rage
Code: Select all
int Function()
{
/*this method is an eminating monstrosity*/
}
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
Code: Select all
int Function()
{
//monstrosity
}
Code: Select all
int Function() {
//utter respectability!
}
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
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!
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
}
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
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!