Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411669 Posts in 69397 Topics- by 58452 Members - Latest Member: homina

May 16, 2024, 11:25:34 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Post your main function(s)
Pages: [1] 2 3 ... 5
Print
Author Topic: Post your main function(s)  (Read 16564 times)
increpare
Guest
« on: January 19, 2009, 05:25:33 PM »

as an aside to the code structuring thread, let's try this for a bit of a laugh.

the kristallnacht main function looks like

Code:
int main(int argc, char *argv[])
{
init();

introloop();

playmusic(1);
while (!quit)
{
titleloop();
prefaceloop();
gameloop();
gameoverloop();
}

return 0;
}


For mirror stage, I was a bit naughty; it looks like

Code:
int main(int argc, char *argv[])
{

Timer fps;

tso=ts_main;
saveasmode=false;

    bool quit = false;

    if( init() == false )
    {
        return 1;
    }

l_choosestate:
quit=false;
wantquit=false;
//curtitleselection1=0;
switch (gamestate)
{
case gs_intro:
glClearColor(0,0,0,1);
goto l_intro;
break;
case gs_title:
glClearColor(0,0,0,1);
goto l_title;
break;
case gs_interlude:
goto l_interlude;
break;
case gs_level:
goto l_level;
break;
case gs_quit:
goto l_quit;
break;
case gs_finishchapter:
goto l_finishchapter;
break;
}

//finish screen
l_finishchapter:
finishchapterloop();
goto l_choosestate;


//intro
l_intro:
introchapterloop();
goto l_choosestate;

//title
l_title:
titlechapterloop();
goto l_choosestate;

l_level:
l_interlude:
    gameloop();
goto l_choosestate;


l_quit:
//Clean up
fadetoblack();
    clean_up();

    return 0;
}

For Rara Racer it looks like*

Code:
int main()
{
        srand(time(0));
        allegro_init();
        init_sound();
       
        init_graphics();
       
        play_music(0);
        play_narration(0);
        blit(bg,buffer,0,0,0,0,screenwidth,screenheight);
     
        install_timer();

        LOCK_VARIABLE(ticks);
        LOCK_FUNCTION(ticker);
        install_int_ex(ticker, BPS_TO_TIMER(updates_per_second));

        LOCK_VARIABLE(game_time);
        LOCK_FUNCTION(game_time_ticker);
        install_int_ex(game_time_ticker, BPS_TO_TIMER(10));//i.e. game time is in tenths of seconds

        bool quit = false;

        int fps = 0;
        int frames_done = 0;
        int old_time = 0;

        while((!key[KEY_ESC]) && (endtimer<30))
        {
                while(ticks == 0)
                {
                        rest(100 / updates_per_second);
                }

                while(ticks > 0)
                {
                        int old_ticks = ticks;

                        DoLogic();

                        ticks--;
                        if(old_ticks <= ticks)
                        break;
                }

                if(game_time - old_time >= 10)//i.e. a second has passed since we last measured the frame rate
                {
                        fps = frames_done;
                        //fps now holds the the number of frames done in the last second
                        //you can now output it using textout_ex et al.

                        //reset for the next second
                        frames_done = 0;
                        old_time = game_time;
                }

                DrawEverything();
                frames_done++;//we drew a frame!
        }
        release_sound();
        return 0;
}

(okay I just factored out init_graphics there*).  I would probably have factored out the main game loop, but the game is so simple there didn't seem to be much point at the time.


*okay I didn't really I just cut it from this post and replaced it with init_graphics

Endless Cavern

Code:
main :: IO()
main = do
 initializeGame
 mainLoop
« Last Edit: January 20, 2009, 03:40:22 AM by increpare » Logged
ஒழுக்கின்மை (Paul Eres)
Level 10
*****


Also known as रिंकू.


View Profile WWW
« Reply #1 on: January 19, 2009, 05:30:46 PM »

Game Maker games don't have main functions (they're object based, each object has its own main function and operates as its own distinct program, and there's no one main function starting the game up or handling game logic, at least not that the developer can alter) so I can't really post the main functions to any of my recent games, even though I'd like to.

But here's about as close as you can get -- here's the step event of my "controlling object", that I use to handle the mouse cursor, the player input, and a lot of the game logic in Immortal Defense.

Code:
global.clock += 1;
if not paused
   pd_time();
input_all();
if (global.phase == LEVEL)
{
   check_all();
   
   if (global.particle_id == 0) or mouse_y > 620
      then mouse_dir = 135;
      else mouse_dir = point_direction(mouse_x, mouse_y, (global.particle_id).x, (global.particle_id).y);
   mx1 = mouse_x - lengthdir_x(33, mouse_dir);
   my1 = mouse_y - lengthdir_y(33, mouse_dir);
   
   if (not global.paused)
      cursor_step();
}
check_phasefade();

In retrospect I should have had a function for that middle junk (basically the mouse cursor aims toward the nearest enemy, unless it's at the bottom of the screen).
« Last Edit: January 19, 2009, 05:39:23 PM by rinkuhero » Logged

cpets
Level 0
**


View Profile WWW
« Reply #2 on: January 19, 2009, 05:37:23 PM »

Code:
def main():
    game = Game()
    game.run()

how mysterious!
Logged
Glaiel-Gamer
Guest
« Reply #3 on: January 19, 2009, 05:40:59 PM »

int main(){
game();
return 0;
}
Logged
Annabelle Kennedy
Awesomesauce
Level 8
*


♥Android Love♥


View Profile
« Reply #4 on: January 19, 2009, 05:42:01 PM »

Code:
int main(int argc, char **argv)
{
 

   //lua_State *myLuaState = lua_open();

  // Connect LuaBind to this lua state
//luabind::open(myLuaState);

// scr.mylua=myLuaState;




bool fullscreen=false;
const SDL_VideoInfo *video = SDL_GetVideoInfo();

SDL_EnableUNICODE(true);
InitGraphics();
InitSound();
ss.loadSound("sfx/clouds2.wav");
ss.loadSound("sfx/clouds3.wav");
ss.loadSound("sfx/clouds.wav");
ss.loadSound("sfx/bird.wav");
ss.loadSound("sfx/water1.wav");
ss.loadSound("sfx/water2.wav");
ss.loadSound("sfx/launch.wav");
ss.loadSound("sfx/air.wav");
ss.loadSound("sfx/air2.wav");
ss.loadSound("sfx/door.wav");
ss.loadSound("sfx/choice.wav");
ss.loadSound("sfx/switch.wav");
ss.loadSound("sfx/wallcrash.wav");
ss.loadSound("sfx/dash.wav");
ss.loadSound("sfx/big_slice.wav");
ss.loadSound("sfx/big_drop.wav");
ss.loadSound("sfx/monster_part_floor.wav");
ss.loadSound("sfx/monster_die.wav");
ss.loadSound("sfx/fall_soft.wav");
ss.loadSound("sfx/fall.wav");
ss.loadSound("sfx/grab.wav");
ss.loadSound("sfx/step.wav");
ss.loadSound("sfx/hurt.wav");
ss.loadSound("sfx/contact_top.wav");
ss.loadSound("sfx/monster_hurt.wav");
ss.loadSound("sfx/step_small.wav");
ss.loadSound("sfx/knife_swipe.wav");
ss.loadSound("sfx/text.wav");
ss.loadSound("sfx/text_next.wav");
ss.loadSound("sfx/step_2.wav");
ss.loadSound("sfx/swoosh.wav");
//scr.load("map/game.txt");


scr.addFileToQueue("map/game.txt");

PP = new particles(tl.returnTexture("gfx/particles.bmp"));
PP->SCR=&scr;

P= new player();
P2= new player_elle(&scr);
//P->init(15,15,tl.returnTexture("gfx/char_v2.bmp"));
P->Map=&cmap;
P->c=&col;
P->pp = PP;
P->sl= &sl;
scr.C=C;
scr.tr=&ts;
scr.pmap=&pmap;
scr.cmap=&cmap;
scr.ml1=&ml1;
scr.G=&G;
scr.ml2=&ml2;
scr.gfont=&gfont;
scr.msgb=&msgb;
scr.sl=&sl;
scr.P=P;
scr.PP=PP;
scr.ss=&ss;
//scr.GLO=&GLO;
scr.W=&W;
scr.npcs=&npcs;
scr.tl=&tl;
scr.in=&In;


//luabind::module(scr.mylua) [
//luabind::def("addNPC", playSong)
//];

sl.set(0,"0,1,2,3,4,5,6,7");
ts.init(tl.returnTexture("gfx/transition.bmp"));
pxtone_Ready(GetSDLWindow(), 2, 22050, 16, 0.1f, TRUE, NULL);


//NPCtype Np = NPC_WINTER;

npcs.C=C;

npcs.MAP = &cmap;
npcs.SCR=&scr;
npcs.PP=PP;


msgb.init(&gfont, scr.gfont2, tl.returnTexture("gfx/window-tileset.bmp"), tl.returnTexture("gfx/portrait.bmp"));
msgb.scr=&scr;
//msgb.updatePos(48, 159,14,4);
msgb.updatePos(48, 159,14,4);
W.scr= &scr;
npcs.PLAYER=P;
P->npcs=&npcs;
Uint32 ticks=SDL_GetTicks();
Uint32 ticks2;

In.SCR=&scr;
In.init(10,2,26,26);
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////


CAM= new camera(SCREEN_W/SCALE/2,SCREEN_H/SCALE/2,SCREEN_W/SCALE,SCREEN_H/SCALE);
CAM->M=P->m;
CAM->MA=&ml1;
CAM->followx=true;
CAM->followy=true;
CAM->SCR=&scr;
npcs.CAM=CAM;
scr.CAM=CAM;
scr.console=false;

///////////////////////////////VARIABLE INITIALIZE//////////////
SDL_Event event;
float count=0;
int go=0;
int dirX=0;
bool jumping=false;
float jumpVel=0;
float slow=0;
bool holding=false;
bool cutsceneEnd=false;
////////////////////////////////////////////////////
/////////////////////////////////////////EVENT LOOP
//scr.addFileToQueue("scripts\\scr.txt");
while (go==0){


while (SDL_PollEvent(&event))
{
go = G.ping_event(event);
//MAP->updatePos(x,y);
}

if(SDL_GetTicks()>ticks+22.0f){
ticks=SDL_GetTicks();


if(!scr.console){
scr.think();
}
//scr.hasControl=true;
P->resetKeys();
//P->rightP=true;

if(ts.done()){
if(G.ping_key(KEY_A)){

}
if(G.ping_key(KEY_Z)){

/*luaL_dofile(
myLuaState,
"data/luascr/test.txt"
  );*/
//P->npcWalkToX(75,0);
}
if(G.ping_key(KEY_X)){
//npcs.moveNpcToX("WINTER2",P->m->x, 1);
//////////createNPC(x,y,ID,script,SCRIPT_TYPE,NPC_TYPE,bitmap)

//P->jump(20);
//scr.addToQueue("createNPC("+its(int(floor((floor(P->m->x/TILE_SIZE))*TILE_SIZE+16)))+","+its(int(floor((floor(P->m->y/TILE_SIZE))*TILE_SIZE)))+",NPC1,null,SCRIPT_NULL,NPC_DORAN,\"gfx/platform_hero.bmp\");");
//ts.blackout();
//CAM->rumble(2,15);
//PP->addparticles(P_ITEM_SPARKLE,1,-25,-25,25,-4,P->m->leftCx()+P->m->cRectW/2,P->m->y-6);
//PP->addparticles(P_ITEM_SPARKLE,4,-5,-5,5,5,P->m->leftCx()+P->m->cRectW/2,P->m->y-6);
//PP->addparticles(P_ITEM_STAR,2,-5,-15,10,10,P->m->leftCx()+P->m->cRectW/2,P->m->y-6);
//scr.addToQueue("addParticle(P_CLOUD_MD,257,165,-5,5,-5,5);");
}

bool sp=false, shp=false;
if(G.ping_key(KEY_SHIFT)){
if(scr.waiting && msgb.done() && cutsceneEnd==false){
scr.waiting=false;
if(scr.caseactive){
scr.ss->playSound("choice.wav");
}else{
scr.ss->playSound("text_next.wav");
}
cutsceneEnd=true;
}
if(scr.hasControl && cutsceneEnd==false){
P->shiftP=true;

P2->shiftP=true;
}
sp=true;
}
if(G.ping_key(KEY_S)){
if(scr.waiting && msgb.done() && cutsceneEnd==false){
scr.waiting=false;
scr.ss->playSound("text_next.wav");
cutsceneEnd=true;
}
if(scr.hasControl && cutsceneEnd==false){
P->sP=true;
P2->sP=true;
}
shp=true;

}
if(scr.waiting){
cutsceneEnd=true;
}
if(!sp && !shp){
cutsceneEnd=false;
}

if(G.ping_key(KEY_TILDE)){

G.clear_buffer();
scr.console= scr.console==true?false:true;
}
if(G.ping_key(KEY_ENTER)){
if(scr.console){
scr.addToFrontOfQueue(G.get_Text(),0);
G.clear_buffer();
}else{

}
}
if(G.ping_key(KEY_LEFT)){

if(scr.hasControl){
P->leftP=true;
P2->leftP=true;
}
}
if(G.ping_key(KEY_RIGHT)){
if(scr.hasControl){
P->rightP=true;
P2->rightP=true;
}
}
if(G.ping_key(KEY_UP)){
if(scr.hasControl){
P->upP=true;
P2->upP=true;
}
}
if(G.ping_key(KEY_DOWN)){
if(scr.hasControl){
P->downP=true;
P2->downP=true;
}
//CAM->moveToPos(-100,-100,1,100);
//CAM->move=false;

//CAM->slow=floor(CAM->x)+0.5;
//CAM->slowy=CAM->y;

}

if(G.ping_key(KEY_SPACE)){
//scr.load("map/game.txt");
//scr.saveNewMap("newmap.thm");
}
}
if(!scr.console){
ts.think();
msgb.think();//////MESSAGEBOX
In.think();
W.think();
CAM->think();//////CAMERA
if(!scr.waiting && !scr.paused){

P->think(); //////player
P2->think(); //////player
npcs.think();//////NPCS


P->m->move();//////MOVEPLAYER
PP->think();///////PARTICLES

}
}
DrawGraphics();

}
SDL_Delay(1);
}
return 0;
// Clean up
SDL_Quit();

return 0;

}

I'm... im so sorry!
Logged
ஒழுக்கின்மை (Paul Eres)
Level 10
*****


Also known as रिंकू.


View Profile WWW
« Reply #5 on: January 19, 2009, 05:43:28 PM »

O_O

Although I actually used to know a guy in the Ohrrpgce community who didn't know how to use functions, so he put his entire game in one function.
Logged

Annabelle Kennedy
Awesomesauce
Level 8
*


♥Android Love♥


View Profile
« Reply #6 on: January 19, 2009, 05:44:51 PM »

Its not really as bad as it looks... im sort of in the process of reorganizing everything but I'm lazy because it works and im still implementing stuff...
Logged
Terry
TIGSource Editor
Level 10
******



View Profile WWW
« Reply #7 on: January 19, 2009, 05:44:57 PM »

Heh, neat topic.

A little currently untitled flash thing I'm working on:
Code:
public function mainloop(e:Event):void {
  gamerender(key, dwgfx, game, obj, help);
  gameinput(key, dwgfx, game, obj, help);
  gamelogic(key, dwgfx, game, obj, help);

  //Stupid event listeners!
  if (key.hasclicked) key.click = false;
}

Slightly more interesting, here's Pathways:
Code:
int main(){
  srand(time(NULL));
  init();
  graphicsclass graphics;

  changesong(0);
  musicon=true;

  //We start in room 0:
  graphics.loadroom(0);

  fade=255; fademode=FADEIN;
  gamestate=TEXTBOX;
  currentscript=0;

  //Starting main loop:
  while(!killgame){
    //Prelogic

    //Render
    switch(gamestate){
      case GAMEMODE: gamemode_render(graphics); break;
      case TEXTBOX: textbox_render(graphics); break;
      case ENDING: ending_render(graphics); break;
    }
    //Input
    if(key[KEY_ESC]) killgame=true;
    switch(gamestate){
      case GAMEMODE: gamemode_input(graphics); break;
      case TEXTBOX: textbox_input(graphics); break;
      case ENDING: ending_input(graphics); break;
    }
    //Logic
    process_music();
    process_glow();
    process_fade();
    switch(gamestate){
      case GAMEMODE: gamemode_logic(graphics); break;
      case TEXTBOX: textbox_logic(graphics); break;
    }
    if(screenresize){
      screenresize=false;
      if(fullscreen){graphics.change_fullscreen();
      }else{graphics.change_windowed();}
    }
    while(check_timer(framerate)<=0) rest(0);
    reset_timer();
  }

  #ifdef DEBUG
  debugfile.close();
  #endif
  return 0;
}
END_OF_MAIN()
Logged

David Pittman
Level 2
**


MAEK GAEM


View Profile WWW
« Reply #8 on: January 19, 2009, 05:51:17 PM »

I like this thread idea. Kudos.

My actual main is a bit cluttered, but effectively just calls Game::Main():
Code:
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
Unused( hPrevInstance );
Unused( lpCmdLine );

// Must be initialized before creating any objects.
MemoryManager::GetDefault().Initialize( 16 * 1024 * 1024 );

Game::SGameInit GameInit;
GameInit.hInstance = hInstance;
GameInit.nCmdShow = nCmdShow;
GameInit.pCommandLine = lpCmdLine;
GameInit.Icon = IDI_ICON1;

Game* pGame = new Game;
pGame->Main( GameInit );
SafeDelete( pGame );

#if BUILD_DEV
Profiler::GetInstance()->Dump( FileStream( "profiler.txt", FileStream::EFM_Write ) );
Profiler::DeleteInstance();
#endif

if( MemoryManager::IsEnabled() )
{
#if BUILD_DEBUG
MemoryManager::GetDefault().Report( FileStream( "memory_exit_report.txt", FileStream::EFM_Write ) );
#endif
DEBUGASSERT( MemoryManager::GetDefault().CheckForLeaks() );
MemoryManager::GetDefault().ShutDown();
}

DEBUGASSERT( _CrtCheckMemory() );
DEBUGASSERT( !_CrtDumpMemoryLeaks() );

return 0;
}

Game::Main is much cleaner:
Code:
void Game::Main( const SGameInit& GameInit )
{
if( Initialize( GameInit ) )
{
PRINTF( "Game has started.\n" );

while( Tick() );

DumpUserConfig( FileStream( "Config/user.cfg", FileStream::EFM_Write ) );
DumpProfileConfig( FileStream( m_ProfileManager->GetConfigFile().CStr(), FileStream::EFM_Write ) );
}
ShutDown();
}
Logged

Mr. Yes
Level 5
*****



View Profile WWW
« Reply #9 on: January 19, 2009, 05:58:13 PM »

Code:
int main()
{
    App.UseVerticalSync(true);
    App.ShowMouseCursor(false);
    App.EnableKeyRepeat(false);
    App.SetFramerateLimit(60);

    std::vector<Screen*> game_screens;
    int current_screen = 0;

    TitleScreen titlescreen;
    GameScreen mainscreen;
    game_screens.push_back(&titlescreen);
    game_screens.push_back(&mainscreen);

    while (current_screen >= 0)
        current_screen = game_screens[current_screen]->RunScreen(App);

    return EXIT_SUCCESS;
}

I used (read: stole) the whole "screens" thing from a tutorial in the SFML Wiki. In later projects I'll probably be changing it to better suit my needs.

In this particular case it was probably unnecessary since I only had the title screen and the game itself. Embarrassed
Logged

increpare
Guest
« Reply #10 on: January 19, 2009, 06:02:47 PM »

Code:
    while (current_screen >= 0)
        current_screen = game_screens[current_screen]->RunScreen(App);
That's cute! Haven't see it before.
Logged
Golds
Loves Juno
Level 10
*


Juno sucks


View Profile WWW
« Reply #11 on: January 19, 2009, 06:04:21 PM »

Rotrix PC/Mac

Code:
int main(int argc, char *argv[])
{
// Init SDL video subsystem
if ( SDL_Init (SDL_INIT_VIDEO) < 0 ) {

        fprintf(stderr, "Couldn't initialize SDL: %s\n",
SDL_GetError());
exit(1);
}

    // Set GL context attributes
    initAttributes ();
    
    // Create GL context
    createSurface (1);
    
    // Get GL context attributes
    printAttributes ();
    
    // Init GL state
    initGL ();
    
    // Draw, get events...
    mainLoop ();

    game.release();
    
    // Cleanup
    SDL_Quit();

    return 0;
}
Logged

@doomlaser, mark johns
Ivan
Owl Country
Level 10
*


alright, let's see what we can see


View Profile
« Reply #12 on: January 19, 2009, 06:23:47 PM »


all my main functions look exactly the same:

Quote
int main(int argc, char *argv[]) {   
   GameApp *app = new GameApp();
   while(app->Update()){}   
   return 1;
}
Logged

http://polycode.org/ - Free, cross-platform, open-source engine.
brog
Level 7
**



View Profile WWW
« Reply #13 on: January 19, 2009, 06:31:38 PM »

Simple:
Code:
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
#ifdef _DEBUG
_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif
main_init();
main_loop();
main_exit();
return 0;
}
« Last Edit: January 19, 2009, 06:40:44 PM by brog » Logged
Gold Cray
Level 10
*****


Gold Cray


View Profile WWW
« Reply #14 on: January 19, 2009, 06:47:09 PM »

My code is not nearly so modular.

Death Geode:

Code:
The message exceeds the maximum allowed length (20000 characters). (It's horrible anyway)

Freedom:
Code:
int main(int argc, char *argv[])

{



  short done = 0;

  short start = 1;

  short x;

  short y;

  short win = 0;



  short endingTimer = 0;

  short startTimer = 0;



  long nextChange = 0;



  long startTime;

  long endTime;



  long scoreVal;

  short digits;

  long digit;



  short xpos = 8;

  short direction = 0;



  Uint32 color1;

  Uint32 color2;



  SDL_Surface *numbers = SDL_LoadBMP("numbers.bmp");



  SDL_Surface *score;



  SDL_Rect letter;

    letter.w = 14;

    letter.h = 23;



  SDL_Rect target;

    target.w = 14;

    target.h = 23;

    target.y = 0;



  SDL_Surface *screen;

  SDL_Event event;

  Uint8* keys;



  SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO);



  screen = SDL_SetVideoMode(642, 182, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);



  for(x = xpos-8; x <= xpos+8; ++x)

    for(y = 50; y < 130; ++y)

      *((Uint32 *)screen->pixels + (y+1)*screen->pitch/4 + (x+1))=0xFFFFFF;



  SDL_WM_SetCaption("Freedom","Freedom");



  while(start == 1)

  {

    startTime = SDL_GetTicks();



    keys = SDL_GetKeyState(NULL);

    SDL_PollEvent(&event);

    if(keys[SDLK_ESCAPE])

    { 

      start = 0;

      keys[SDLK_ESCAPE]=0;

    }



    if(startTimer <45)

      for(x = 0; x < 640; ++x)

      {

        *((Uint32 *)screen->pixels + (startTimer+1)*screen->pitch/4 + (x+1))=0xFFFFFF;

        *((Uint32 *)screen->pixels + (180-startTimer)*screen->pitch/4 + (x+1))=0xFFFFFF;

      }



    if(startTimer == 50)

      start=0;



    ++startTimer;



    SDL_Flip(screen);



    endTime = SDL_GetTicks();

    if(endTime - startTime < FRAMERATE)

    SDL_Delay(FRAMERATE - (endTime - startTime));

   

  }



  nextChange = SDL_GetTicks() + 1000;



  while(!done)

  {

    startTime = SDL_GetTicks();



    keys = SDL_GetKeyState(NULL);

    SDL_PollEvent(&event);

    if(keys[SDLK_ESCAPE])   

      done = 1;       



    if(startTime >= nextChange)

    {

      ++direction;

      if(direction>1)

        direction = 0;

      nextChange = startTime + rand()/(float)RAND_MAX * 2250 + 250;

    }



    if(keys[SDLK_RIGHT] && direction == 0)

      ++xpos;

    else  if(keys[SDLK_RIGHT] && direction == 1)

      xpos-=2;



    if(direction == 0)

    {

      color1 = 0xFFFFFF;

      color2 = 0;

    }

    else

    {

      color1 = 0;

      color2 = 0xFFFFFF;

    }



    for(x = 0; x < 640; ++x)

      for(y = 0; y < 45; ++y)

      {

        *((Uint32 *)screen->pixels + (y+1)*screen->pitch/4 + (x+1))=color1;

        *((Uint32 *)screen->pixels + (y+46)*screen->pitch/4 + (x+1))=color2;

        *((Uint32 *)screen->pixels + (y+91)*screen->pitch/4 + (x+1))=color2;

        *((Uint32 *)screen->pixels + (y+136)*screen->pitch/4 + (x+1))=color1;

      }



    if(xpos < 8)

      xpos = 8;

    if(xpos > 631)

      xpos = 631;



    for(x = xpos-8; x <= xpos+8; ++x)

      for(y = 50; y < 130; ++y)

        *((Uint32 *)screen->pixels + (y+1)*screen->pitch/4 + (x+1))=color1;



    if(xpos == 631)

    {

      done = 1;

      win = 1;

      cout << startTime << endl;

      scoreVal = startTime;

      for(x = 0; x < 640; ++x)

        for(y = 0; y < 45; ++y)

        {

          *((Uint32 *)screen->pixels + (y+1)*screen->pitch/4 + (x+1))=0xFFFFFF;

          *((Uint32 *)screen->pixels + (y+46)*screen->pitch/4 + (x+1))=0;

          *((Uint32 *)screen->pixels + (y+91)*screen->pitch/4 + (x+1))=0;

          *((Uint32 *)screen->pixels + (y+136)*screen->pitch/4 + (x+1))=0xFFFFFF;

          for(x = xpos-8; x <= xpos+8; ++x)

            for(y = 50; y < 130; ++y)

              *((Uint32 *)screen->pixels + (y+1)*screen->pitch/4 + (x+1))=0xFFFFFF;

        }

      digits = (short)(floor(log(scoreVal)/log(10))+1);

      score = SDL_CreateRGBSurface(SDL_HWSURFACE|SDL_SRCCOLORKEY, 14*digits, 23, 32, 0, 0, 0, 0);

      SDL_SetColorKey(score, SDL_SRCCOLORKEY, SDL_MapRGB(score->format, 255, 255, 255));

      for(x = 0; x < digits; ++x)

      {

        digit = ((long)floor(scoreVal/pow((double)10.0,x))%10);

        letter.x = (short)(14*(digit%5));

        letter.y = (short)(23*floor(digit/5));

        target.x = 14*(digits-x-1);

        SDL_BlitSurface(numbers, &letter, score, &target);

      }

      letter.w = score->w;

      letter.h = score->h;

      letter.x = 0;

      letter.y = 0;



      target.w = score->w;

      target.h = score->h;

      target.x = 1+(640-letter.w)/2;

      target.y = 79;



    }



    SDL_Flip(screen);



    endTime = SDL_GetTicks();

    if(endTime - startTime < FRAMERATE)

    SDL_Delay(FRAMERATE - (endTime - startTime));



  }



  while(win == 1)

  {

    startTime = SDL_GetTicks();



    keys = SDL_GetKeyState(NULL);

    SDL_PollEvent(&event);

    if(keys[SDLK_ESCAPE])   

      win = 0;



    if(endingTimer <= 45)

    {

      for(x = 0; x < 640; ++x)

      {

        *((Uint32 *)screen->pixels + (46+endingTimer)*screen->pitch/4 + (x+1))=0xFFFFFF;

        *((Uint32 *)screen->pixels + (136-endingTimer)*screen->pitch/4 + (x+1))=0xFFFFFF;

      }

      SDL_BlitSurface(score, &letter, screen, &target);

    }

   

    if(endingTimer == 70)

      win=0;



    ++endingTimer;



    SDL_Flip(screen);



    endTime = SDL_GetTicks();

    if(endTime - startTime < FRAMERATE*2)

    SDL_Delay(2*FRAMERATE - (endTime - startTime));



   

  }



  SDL_Quit();

 

  return 0;





}

Logged
Problem Machine
Level 8
***

It's Not a Disaster


View Profile WWW
« Reply #15 on: January 19, 2009, 07:39:00 PM »

Current test main for my sprite class:
Code:
int main(int argc, char* args[])
{
bool quit = false;

if(!initGraphics(800, 600, 32))
return 1;

Sprite* demo1 = createSprite(0.2, 0.2, "Demonstration Sprite", &Sprite::getStage());
demo1->setX(0.5).setY(0.5);
Sprite* demo2 = createSprite(0.1, 0.1, "Second Demonstration Sprite", demo1);
demo2->setX(0.2).setY(0.2);
Sprite* demo3 = createSprite(0.03, 0.03, "Third Demonstration Sprite", demo2);
demo3->setY(-0.2);

while( quit == false )
{
while( SDL_PollEvent( &event ) )
{
if( event.type == SDL_QUIT )
{
quit = true;
}
}

demo1->rotateDegrees(0.3);
demo2->rotateDegrees(0.6);
demo3->rotateDegrees(0.9);
renderWorld();
}

Sprite::destroyAll();

return 0;
}

And the equivalent function for my currently shelved AS project:
Code:
private function frameUpdate(e:Event):void
{
ControllerManager.checkForAllInputs();
EntityManager.updateAllEntities();
}

The C++ one obviously needs some work  Concerned
« Last Edit: January 19, 2009, 07:42:15 PM by Kobel » Logged

Hideous
That's cool.
Level 10
*****


3D models are the best


View Profile WWW
« Reply #16 on: January 19, 2009, 08:40:24 PM »

This thread makes me feel bad for my main function - I basically just stuff it in there Sad
Logged

Robotacon
Pixelhead
Level 3
******


Story mode


View Profile
« Reply #17 on: January 20, 2009, 12:14:46 AM »

Code:
int main( int argc, char* args[] ) { 
  if(init()) {
    load("find the armory");
    play();
    reset();
  } else {
    return 1;
  }
  SDL_Quit();
  return 0;
}

Ps. Am I the only one fighting against C++ style curly-brackets-on-a-new-line? I can't stand it.
« Last Edit: January 20, 2009, 12:20:43 AM by robotacon » Logged
Gravious
Level 2
**


"Swedish meatballs"


View Profile WWW
« Reply #18 on: January 20, 2009, 12:53:11 AM »

Code:
int main( int argc, char* args[] ) { 
  if(init()) {
    load("find the armory");
    play();
    reset();
  } else {
    return 1;
  }
  SDL_Quit();
  return 0;
}

Ps. Am I the only one fighting against C++ style curly-brackets-on-a-new-line? I can't stand it.

robotacon: no!  I hate that too...

My main() is a mess at the moment, cause I'm engine building so i wont post it.
Logged

One day I'll think about doing something to stop procrastinating.
Alex May
...is probably drunk right now.
Level 10
*


hen hao wan


View Profile WWW
« Reply #19 on: January 20, 2009, 12:59:30 AM »

Code:
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            mInstance = new Game();
            mInstance.Go();
        }
Logged

Pages: [1] 2 3 ... 5
Print
Jump to:  

Theme orange-lt created by panic