我正在尝试学习c ++和sdl,但到目前为止,我的失败非常惨淡。我一直在关注sdl上的Lazy Foo教程,但在他的所有教程中,代码都被转储到一个大的主要源文件中。我正在寻找一种方法来分割和分割这大块代码到头和类中,这样我的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: %s\n", 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: %s\n", 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: %s\n", "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();
}
lazy 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: %s\n", 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: %s\n", 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: %s\n", "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;
}
所有和任何帮助表示赞赏,谢谢;)
答案 0 :(得分:0)
大项目被分成很多文件,通常映射到目录层次结构。这样做的原因不是效率(如果将整个代码转储到单个文件中并且它根本不会影响运行时间,则编译实际上可能更快),但代码管理问题(想象打开超过100 000行的文件)代码)。
编程语言和工具通常用于帮助实践。例如,在C / C ++中,您有include
预处理程序指令,它允许您“导入”文件。
关于如何执行实际拆分的问题,通常最好不要使用像你这样的自上而下的方法(即我有代码,如何拆分它?)而是设计具有这种模块化的代码心神。话虽如此,您可以将每个文件视为一个小模块,它提供了一些相关的功能。这里有很多事情需要考虑,开发应用程序的架构有点艺术,但就目前而言,只需在同一个地方保留具有类似功能的东西就是一个好的开始。
你的例子对于任何实际的建议都太小了,它太小了,所以在一个文件中包含所有东西并不是那么糟糕。你的师很好,你把游戏逻辑分开了(我真的不喜欢你的模块名game_state
,因为你并没有真正代表那里的游戏状态;你似乎定义了一些游戏相关的过程)但并非真的有必要。