如何将这些代码分段为标头、源文件和类
How to segment this code into headers and source files and classes?
我正在努力学习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
的名字,因为你并没有真正代表那里的游戏状态;你似乎定义了一些与游戏相关的过程),但这并不是真正必要的。
- 分段故障(堆芯转储)矢量
- 数组的指针从不分段故障
- 在某些循环内使用vector.push_back时出现分段错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 在线编译器中的分段C++没有打印消息
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 当我的阵列太大时出现分段错误
- Windows 10-使用gtkmm-3.0库和g++[包括再现]的分段故障
- 分段错误当我试图运行程序时出错
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 为测试目标创建具有不同源文件夹的文件
- 尝试使用集合函数时出现分段错误
- 我无法缩小此分段错误的原因
- 将图像添加到资源文件夹UWP C++
- g++的分段错误(在NaN上使用to_string两次时)
- 我是如何在这段代码中出现分段错误的
- 创建结构的数组时遇到分段错误
- 在c++中键入向量中的所有值后,得到分段错误(核心转储)
- 视觉工作室项目.提取源文件夹名称
- 如何将这些代码分段为标头、源文件和类