如何将这些代码分段为标头、源文件和类

  • 本文关键字:源文件 分段 代码 c++ sdl
  • 更新时间 :
  • 英文 :


我正在努力学习c++和sdl,但到目前为止我失败得很惨。我一直在关注Lazy Foo关于sdl的教程,但在他的所有教程中,代码都被转储到一个大的主源文件中。我正在寻找一种方法,将这大块代码分割成头和类,这样我的main.cpp文件将只有一个int main函数,所有其他init和exit函数都将留在其他文件中。我不知道这是否是一个好的做法,但到目前为止,我已经读到你应该把你的代码分成几个部分,这样它运行得更快、更流畅。如果你能给我看一些例子,我相信我能弄清楚剩下的。以下是我的代码和我尝试过的分区方法:



main.cpp

#include <SDL.h>
#include <stdio.h>
#include "game_state.h"
#include "game_state.cpp"
int main( int argc, char* args[] ) {
    if( !init() )
    {
        printf( "Failed to initialize!n" );
    } else {
        //Load media
        if( !loadMedia() )
        {
            printf( "Failed to load media!n" );
        } else {
            //Apply the image
            SDL_BlitSurface( gHelloWorld, NULL, gScreenSurface, NULL );
            //Update the surface
            SDL_UpdateWindowSurface( gWindow );
            //Wait two seconds
            SDL_Delay( 2000 );
        }
    }
    //Free resources and close SDL
    onExit();
    return 0;
}

game_state.h

#ifndef PLAYER_H
#define PLAYER_H
#include <SDL.h>
#include <stdio.h>
//The window we'll be rendering to
SDL_Window *gWindow;
//The surface contained by the window
SDL_Surface *gScreenSurface;
//The image we will load and show on the screen
SDL_Surface *gHelloWorld;
bool init();
bool loadMedia();
void onExit();
#endif

game_state.cpp

#include "game_state.h"

//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;


bool init() {
    //Initialization flag
    bool success = true;
    //Initialize SDL
    if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
    {
        printf( "SDL could not initialize! SDL_Error: %sn", SDL_GetError() );
        success = false;
    }
    else
    {
        //Create window
        gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
        if( gWindow == NULL )
        {
            printf( "Window could not be created! SDL_Error: %sn", SDL_GetError() );
            success = false;
        }
        else
        {
            //Get window surface
            gScreenSurface = SDL_GetWindowSurface( gWindow );
        }
    }
    return success;
}
bool loadMedia() {
    //Loading success flag
    bool success = true;
    //Load splash image
    gHelloWorld = SDL_LoadBMP( "02_getting_an_image_on_the_screen/hello_world.bmp" );
    if( gHelloWorld == NULL )
    {
        printf( "Unable to load image %s! SDL Error: %sn", "02_getting_an_image_on_the_screen/hello_world.bmp", SDL_GetError() );
        success = false;
    }
    return success;
}
void onExit()
{
    //Deallocate surface
    SDL_FreeSurface( gHelloWorld );
    gHelloWorld = NULL;
    //Destroy window
    SDL_DestroyWindow( gWindow );
    gWindow = NULL;
    //Quit SDL subsystems
    SDL_Quit();
}

懒惰的foo的原始main.cpp,我试图将其分段:

//Using SDL and standard IO
#include <SDL.h>
#include <stdio.h>
//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
//Starts up SDL and creates window
bool init();
//Loads media
bool loadMedia();
//Frees media and shuts down SDL
void close();
//The window we'll be rendering to
SDL_Window* gWindow = NULL;
//The surface contained by the window
SDL_Surface* gScreenSurface = NULL;
//The image we will load and show on the screen
SDL_Surface* gHelloWorld = NULL;
bool init()
{
    //Initialization flag
    bool success = true;
    //Initialize SDL
    if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
    {
        printf( "SDL could not initialize! SDL_Error: %sn", SDL_GetError() );
        success = false;
    }
    else
    {
        //Create window
        gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
        if( gWindow == NULL )
        {
            printf( "Window could not be created! SDL_Error: %sn", SDL_GetError() );
            success = false;
        }
        else
        {
            //Get window surface
            gScreenSurface = SDL_GetWindowSurface( gWindow );
        }
    }
    return success;
}
bool loadMedia()
{
    //Loading success flag
    bool success = true;
    //Load splash image
    gHelloWorld = SDL_LoadBMP( "02_getting_an_image_on_the_screen/hello_world.bmp" );
    if( gHelloWorld == NULL )
    {
        printf( "Unable to load image %s! SDL Error: %sn", "02_getting_an_image_on_the_screen/hello_world.bmp", SDL_GetError() );
        success = false;
    }
    return success;
}
void close()
{
    //Deallocate surface
    SDL_FreeSurface( gHelloWorld );
    gHelloWorld = NULL;
    //Destroy window
    SDL_DestroyWindow( gWindow );
    gWindow = NULL;
    //Quit SDL subsystems
    SDL_Quit();
}
int main( int argc, char* args[] )
{
    //Start up SDL and create window
    if( !init() )
    {
        printf( "Failed to initialize!n" );
    }
    else
    {
        //Load media
        if( !loadMedia() )
        {
            printf( "Failed to load media!n" );
        }
        else
        {
            //Apply the image
            SDL_BlitSurface( gHelloWorld, NULL, gScreenSurface, NULL );
            //Update the surface
            SDL_UpdateWindowSurface( gWindow );
            //Wait two seconds
            SDL_Delay( 2000 );
        }
    }
    //Free resources and close SDL
    close();
    return 0;
}

感谢所有的帮助,谢谢;)

大型项目被拆分为许多文件,这些文件通常映射到目录层次结构中。造成这种情况的原因不是效率(如果将整个代码转储到一个文件中,并且完全不影响运行时间,那么编译实际上可能会更快),而是代码管理问题(想象一下打开一个包含超过100000行代码的文件)。

编程语言和工具通常是为了帮助这种实践而设计的。例如,在C/C++中,您有include预处理器指令,该指令允许您"导入"文件。

关于如何执行实际拆分的问题,通常最好不要使用像您这样的自上而下的方法(即,我有代码,如何拆分?),而是在设计代码时考虑到这种模块性。话虽如此,您可以将每个文件视为一个小模块,提供一些相关功能。这里有很多事情需要考虑,开发应用程序的体系结构有点像一门艺术,但就目前而言,将具有类似功能的东西放在同一个地方是一个良好的开端。

你的例子太小了,不适合任何实用的建议,它太小了以至于把所有东西都放在一个文件里也没那么糟糕。你的划分很好,你分离了游戏逻辑(我真的不喜欢你的模块game_state的名字,因为你并没有真正代表那里的游戏状态;你似乎定义了一些与游戏相关的过程),但这并不是真正必要的。

最新更新