⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 g19-02.cpp

📁 游戏开发数据结构-Data.Structures.for.Game.Programmers
💻 CPP
📖 第 1 页 / 共 2 页
字号:

                // loop through each layer, and set the tile information.
                for( z = 0; z < 4; z++ )
                {
                    entry.layers[z] = g_map.Get( x, y, z );
                }

                // finally, write the cell to disk
                fwrite( &entry, 1, sizeof(MapEntry), f );
            }
        }
    }

    // write the exits.
    fwrite( g_exits[0], 64, sizeof(char), f );
    fwrite( g_exits[1], 64, sizeof(char), f );
    fwrite( g_exits[2], 64, sizeof(char), f );

    // close the file
    fclose( f );
}

void Load()
{
    MapEntry entry;
    int x, y, z;
    int cells;

    // clear the map entirely
    for( z = 0; z < 4; z++ )
    {
        for( y = 0; y < g_map.Height(); y++ )
        {
            for( x = 0; x < g_map.Width(); x++ )
            {
                g_map.Get( x, y, z ) = -1;
            }
        }
    }


    // open the file
    FILE* f = fopen( g_filename, "rb" );
    
    // if file could not be opened, return
    if( f == 0 )
        return;

    // read in the type of map 
    fread( &x, 1, sizeof(int), f );

    // if this is an invalid map type, return.
    if( x != 1 )
    {
        fclose( f );
        return;
    }

    // read in the number of cells
    fread( &cells, 1, sizeof(int), f );

    // now go through each cell and load it in.
    for( x = 0; x < cells; x++ )
    {
        // read in the cell
        fread( &entry, 1, sizeof(MapEntry), f );

        // go through each layer in the current cell
        for( z = 0; z < 4; z++ )
        {
            // copy the tile into the current cell
            g_map.Get( entry.x, entry.y, z ) = entry.layers[z];
        }
    }

    // read the exits.
    fread( g_exits[0], 64, sizeof(char), f );
    fread( g_exits[1], 64, sizeof(char), f );
    fread( g_exits[2], 64, sizeof(char), f );

    // close the file
    fclose( f );

}





void Stone()   { g_currentgroup = 0; }
void Dirt()    { g_currentgroup = 1; }
void Dirt2()    { g_currentgroup = 2; }
void Dirt3()    { g_currentgroup = 3; }
void Items()    { g_currentgroup = 4; }
void Players()  { g_currentgroup = 5; }
void Exit()     { g_currentgroup = 6; }


void Clear0()    
{ 
    g_currenttile = -1; 
    g_currentlayer = 0;
}

void Clear1()    
{ 
    g_currenttile = -1; 
    g_currentlayer = 1;
}

void Clear2()
{ 
    g_currenttile = -1; 
    g_currentlayer = 2;
}

void Clear3()
{ 
    g_currenttile = -1; 
    g_currentlayer = 3;
}

// ============================================================================
//  Main
// ============================================================================
int main( int argc, char* argv[] )
{
    int x;
    int y;
    int z;

    //initialize systems
    SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER );
    SDL_EnableUNICODE( true );
    SDL_WM_SetCaption( PROGRAM_NAME, 0); 
    TTF_Init();
    g_gui = new SDLGUI( WIDTH, HEIGHT, ITEMS, BLACK );
    g_gui->SetFont( "arial.ttf", ARIAL, 18, TTF_STYLE_NORMAL );

    
    g_gui->AddCheckbox( 512, 440, "checkboxup.bmp", "checkboxdown.bmp",
                        "Highlight Current", ARIAL, WHITE, BLACK, Highlight );
    g_gui->AddTextBox( 512, 568, 159, 31, g_filename, 63, 
                       ARIAL, WHITE, BLACK, true, 0 );

    g_gui->AddLabel( 512, 472, "red map:", ARIAL, WHITE, BLACK );
    g_gui->AddTextBox( 610, 472, 189, 31, g_exits[0], 63, 
                       ARIAL, WHITE, BLACK, true, 0 );
    g_gui->AddLabel( 512, 504, "green map:", ARIAL, WHITE, BLACK );
    g_gui->AddTextBox( 610, 504, 189, 31, g_exits[1], 63, 
                       ARIAL, WHITE, BLACK, true, 0 );
    g_gui->AddLabel( 512, 536, "blue map:", ARIAL, WHITE, BLACK );
    g_gui->AddTextBox( 610, 536, 189, 31, g_exits[2], 63, 
                       ARIAL, WHITE, BLACK, true, 0 );

    g_gui->AddButton( 672, 568, "gbup2.bmp", "gbdown2.bmp", "Save", 
                      ARIAL, BLACK, WHITE, Save );
    g_gui->AddButton( 736, 568, "gbup2.bmp", "gbdown2.bmp", "Load", 
                      ARIAL, BLACK, WHITE, Load );

    g_gui->AddButton( 512, 0, "gbup.bmp", "gbdown.bmp", "Stone", 
                      ARIAL, BLACK, WHITE, Stone );
    g_gui->AddButton( 512, 32, "gbup.bmp", "gbdown.bmp", "Dirt", 
                      ARIAL, BLACK, WHITE, Dirt );
    g_gui->AddButton( 512, 64, "gbup.bmp", "gbdown.bmp", "Dirt2", 
                      ARIAL, BLACK, WHITE, Dirt2 );
    g_gui->AddButton( 512, 96, "gbup.bmp", "gbdown.bmp", "Dirt3", 
                      ARIAL, BLACK, WHITE, Dirt3 );
    g_gui->AddButton( 512, 128, "gbup.bmp", "gbdown.bmp", "Items",
                      ARIAL, BLACK, WHITE, Items );
    g_gui->AddButton( 512, 160, "gbup.bmp", "gbdown.bmp", "Players",
                      ARIAL, BLACK, WHITE, Players );
    g_gui->AddButton( 512, 192, "gbup.bmp", "gbdown.bmp", "Exit",
                      ARIAL, BLACK, WHITE, Exit );
    g_gui->AddButton( 512, 256, "gbup.bmp", "gbdown.bmp", "del base",
                      ARIAL, BLACK, WHITE, Clear0 );
    g_gui->AddButton( 512, 288, "gbup.bmp", "gbdown.bmp", "del tile",
                      ARIAL, BLACK, WHITE, Clear1 );
    g_gui->AddButton( 512, 320, "gbup.bmp", "gbdown.bmp", "del item",
                      ARIAL, BLACK, WHITE, Clear2 );
    g_gui->AddButton( 512, 352, "gbup.bmp", "gbdown.bmp", "del player",
                      ARIAL, BLACK, WHITE, Clear3 );


    // load the bmps

    // layer 0 bmps:

    // Stone
    g_groupstarts[0] = 0;
    g_groupcounts[0] = 1;
    g_grouplayers[0] = 0;
    g_bmps[0][0][0] = SDL_LoadBMP( "stone.bmp" );
    g_bmps[0][0][1] = SDL_LoadBMP( "sstone.bmp" );

    // dirt
    g_groupstarts[1] = 1;
    g_groupcounts[1] = 1;
    g_grouplayers[1] = 0;
    g_bmps[0][1][0] = SDL_LoadBMP( "dirt.bmp" );
    g_bmps[0][1][1] = SDL_LoadBMP( "sdirt.bmp" );

    // dirt2
    g_groupstarts[2] = 2;
    g_groupcounts[2] = 4;
    g_grouplayers[2] = 1;
    g_bmps[0][2][0] = SDL_LoadBMP( "dirtl.bmp" );
    g_bmps[0][2][1] = SDL_LoadBMP( "sdirtl.bmp" );
    g_bmps[0][3][0] = SDL_LoadBMP( "dirtr.bmp" );
    g_bmps[0][3][1] = SDL_LoadBMP( "sdirtr.bmp" );
    g_bmps[0][4][0] = SDL_LoadBMP( "dirtt.bmp" );
    g_bmps[0][4][1] = SDL_LoadBMP( "sdirtt.bmp" );
    g_bmps[0][5][0] = SDL_LoadBMP( "dirtb.bmp" );
    g_bmps[0][5][1] = SDL_LoadBMP( "sdirtb.bmp" );


    // layer 1 bmps:

    // dirt3
    g_groupstarts[3] = 6;
    g_groupcounts[3] = 8;
    g_grouplayers[3] = 1;
    g_bmps[0][6][0] = SDL_LoadBMP( "dirtitl.bmp" );
    g_bmps[0][6][1] = SDL_LoadBMP( "sdirtitl.bmp" );
    g_bmps[0][7][0] = SDL_LoadBMP( "dirtitr.bmp" );
    g_bmps[0][7][1] = SDL_LoadBMP( "sdirtitr.bmp" );
    g_bmps[0][8][0] = SDL_LoadBMP( "dirtibl.bmp" );
    g_bmps[0][8][1] = SDL_LoadBMP( "sdirtibl.bmp" );
    g_bmps[0][9][0] = SDL_LoadBMP( "dirtibr.bmp" );
    g_bmps[0][9][1] = SDL_LoadBMP( "sdirtibr.bmp" );
    g_bmps[0][10][0] = SDL_LoadBMP( "dirtotl.bmp" );
    g_bmps[0][10][1] = SDL_LoadBMP( "sdirtotl.bmp" );
    g_bmps[0][11][0] = SDL_LoadBMP( "dirtotr.bmp" );
    g_bmps[0][11][1] = SDL_LoadBMP( "sdirtotr.bmp" );
    g_bmps[0][12][0] = SDL_LoadBMP( "dirtobl.bmp" );
    g_bmps[0][12][1] = SDL_LoadBMP( "sdirtobl.bmp" );
    g_bmps[0][13][0] = SDL_LoadBMP( "dirtobr.bmp" );
    g_bmps[0][13][1] = SDL_LoadBMP( "sdirtobr.bmp" );


    // items
    g_groupstarts[4] = 6;
    g_groupcounts[4] = 8;
    g_grouplayers[4] = 2;
    g_bmps[1][6][0] = SDL_LoadBMP( "wepicon-axe.bmp" );
    g_bmps[1][7][0] = SDL_LoadBMP( "wepicon-bow.bmp" );
    g_bmps[1][8][0] = SDL_LoadBMP( "wepicon-knife.bmp" );
    g_bmps[1][9][0] = SDL_LoadBMP( "wepicon-mace.bmp" );
    g_bmps[1][10][0] = SDL_LoadBMP( "wepicon-spear.bmp" );
    g_bmps[1][11][0] = SDL_LoadBMP( "wepicon-sword.bmp" );
    g_bmps[1][12][0] = SDL_LoadBMP( "wepicon-helm.bmp" );
    g_bmps[1][13][0] = SDL_LoadBMP( "wepicon-shield.bmp" );
    g_bmps[1][6][1] = SDL_LoadBMP( "swepicon-axe.bmp" );
    g_bmps[1][7][1] = SDL_LoadBMP( "swepicon-bow.bmp" );
    g_bmps[1][8][1] = SDL_LoadBMP( "swepicon-knife.bmp" );
    g_bmps[1][9][1] = SDL_LoadBMP( "swepicon-mace.bmp" );
    g_bmps[1][10][1] = SDL_LoadBMP( "swepicon-spear.bmp" );
    g_bmps[1][11][1] = SDL_LoadBMP( "swepicon-sword.bmp" );
    g_bmps[1][12][1] = SDL_LoadBMP( "swepicon-helm.bmp" );
    g_bmps[1][13][1] = SDL_LoadBMP( "swepicon-shield.bmp" );

    // players
    g_groupstarts[5] = 0;
    g_groupcounts[5] = 3;
    g_grouplayers[5] = 3;
    g_bmps[2][0][0] = SDL_LoadBMP( "hero1.bmp" );
    g_bmps[2][1][0] = SDL_LoadBMP( "hero2.bmp" );
    g_bmps[2][2][0] = SDL_LoadBMP( "hero3.bmp" );
    g_bmps[2][0][1] = SDL_LoadBMP( "shero1.bmp" );
    g_bmps[2][1][1] = SDL_LoadBMP( "shero2.bmp" );
    g_bmps[2][2][1] = SDL_LoadBMP( "shero3.bmp" );


    // exits
    g_groupstarts[6] = 14;
    g_groupcounts[6] = 3;
    g_grouplayers[6] = 2;
    g_bmps[1][14][0] = SDL_LoadBMP( "rvortex.bmp" );
    g_bmps[1][15][0] = SDL_LoadBMP( "gvortex.bmp" );
    g_bmps[1][16][0] = SDL_LoadBMP( "bvortex.bmp" );
    g_bmps[1][14][1] = SDL_LoadBMP( "srvortex.bmp" );
    g_bmps[1][15][1] = SDL_LoadBMP( "sgvortex.bmp" );
    g_bmps[1][16][1] = SDL_LoadBMP( "sbvortex.bmp" );


    // fill the map with blanks.
    for( y = 0; y < g_map.Height(); y++ )
    {
        for( x = 0; x < g_map.Width(); x++ )
        {
            g_map.Get( x, y, 0 ) = -1;
            g_map.Get( x, y, 1 ) = -1;
            g_map.Get( x, y, 2 ) = -1;
            g_map.Get( x, y, 3 ) = -1;
        }
    }


    // set the color keys of the BMPs
    for( y = 0; y < 3; y++ )
    {
        for( x = 0; x < 41; x++ )
        {
            if( g_bmps[y][x][0] != 0 )
            {
                SDL_SetColorKey( g_bmps[y][x][0], SDL_SRCCOLORKEY, 
                                 SDL_MapRGB( g_bmps[y][x][0]->format, 
                                             255, 0, 255 ) );
                SDL_SetColorKey( g_bmps[y][x][1], SDL_SRCCOLORKEY, 
                                 SDL_MapRGB( g_bmps[y][x][1]->format, 
                                             255, 0, 255 ) );
            }
        }
    }
    
    // set our at exit function
    atexit( SDL_Quit ) ;

    // declare event variable
    SDL_Event event;

    g_gui->Draw();
    g_gui->Update();

    // loop until we get a quit message.
    while( 1 )
    {
        //look for an event
        if( SDL_PollEvent( &event ) )
        {
            //an event was found
            if( event.type == SDL_QUIT ) 
                break;

            // mouse button was pressed
            if( event.type == SDL_MOUSEBUTTONDOWN ) 
            {
                // get the mouse g_state.
                SDL_GetMouseState( &x, &y );

                // tell the GUI that a button has been pressed
                g_gui->MouseDown( x, y );

                // check to see if the user clicked on a new tile in 
                // the palette
                if( y >= 536 && x <= 512 )
                {
                    z = x / 64;

                    // make sure that the user actually clicked
                    // on a tile, instead of a blank.
                    if( z < g_groupcounts[g_currentgroup] )
                    {
                        g_currenttile = g_groupstarts[g_currentgroup] + z;
                        g_currentlayer = g_grouplayers[g_currentgroup];
                    }
                }

                g_mousedown = true;

            }


            // mouse button was released
            if( event.type == SDL_MOUSEBUTTONUP ) 
            {
                // get the mouse state.
                SDL_GetMouseState( &x, &y );

                // tell the GUI that a button has been released
                g_gui->MouseUp( x, y );

                g_mousedown = false;
                // see if the user clicked on a square
                x = x / 8;
                y = y / 8;

                // draw the tile, in case the user didn't move the mouse.
                DrawTile( x, y );
            }

            if( event.type == SDL_MOUSEMOTION )
            {
                // get the mouse state.
                SDL_GetMouseState( &x, &y );

                // see if the user clicked on a square
                if( g_mousedown == true )
                {
                    x = x / 8;
                    y = y / 8;
                    DrawTile( x, y );
                }
            }

            if( event.type == SDL_KEYDOWN )
            {
                // get the mouse state.
                SDL_GetMouseState( &x, &y );

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


                // tell the GUI that a key was pressed.
                g_gui->KeyDown( event.key.keysym.sym, 
                                event.key.keysym.mod,
                                event.key.keysym.unicode );
            }
        }

        // draw the g_gui at the end of each loop.
        g_gui->Draw();

        DrawMap();
        DrawMiniMap();
        DrawPalette();

        g_gui->Update();
    }

    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -