我正在使用 SDL2 和 C 开发这个图形/游戏引擎。
我设法编写了游戏循环的所有基础代码,并将内容分成多个文件。当我尝试定义自己的数据类型(在本例中为结构)时出现了问题。我在 tad.h 和 tad.c 中有我的 struct (vector3) 定义和声明,但在使用此 vector3 类型创建变量时遇到问题。我尝试在主文件中声明此 ponto1,并且尝试在其他文件中将其用作全局变量,但出现一些错误。
我希望能够声明我自己的结构体和指向结构体的指针,但我是在 C 中使用多个文件这样的新手。以下是我收到的文件和问题: (PS:也欢迎有关如何更好地构建我的程序的建议)
tad.h:
#ifndef TAD
#define TAD
typedef struct vector3 Vector3;
#endif
tad.c:
#include <stdio.h>
#include <SDL2/SDL.h>
#include "./tad.h"
#include "./constants.h"
#include "./globals.h"
struct vector3 {
float x;
float y;
float z;
};
globals.h:
extern SDL_Window* my_window;
extern SDL_Renderer* my_renderer;
extern int game_is_running;
extern Vector3 ponto1;
main.c
#include <stdio.h>
#include <SDL2/SDL.h>
#include "./constants.h"
#include "./globals.h"
#include "./tad.h"
#include "./init_and_destroy.h"
#include "./setup.h"
#include "./process_input.h"
#include "./update.h"
#include "./render.h"
SDL_Window* my_window = NULL;
SDL_Renderer* my_renderer = NULL;
int game_is_running = FALSE;
Vector3 ponto1;
int main() {
game_is_running = initialize_window(&my_window, &my_renderer);
setup();
while(game_is_running) {
process_input();
update();
render();
}
destroy_window(my_window, my_renderer);
return 0;
}
更新.c
#include "./constants.h"
#include "./globals.h"
#include "./tad.h"
#include "./update.h"
int last_frame_time = 0;
void update() {
//caping frame rate
int time_to_wait = FRAME_TARGET_TIME - (SDL_GetTicks() - last_frame_time);
if (time_to_wait > 0 && time_to_wait <= FRAME_TARGET_TIME) {
SDL_Delay(time_to_wait);
}
//delta time in seconds
float delta_time = (SDL_GetTicks() - last_frame_time) / 1000.0f;
last_frame_time = SDL_GetTicks();
//-------------------------------------------------------------------------------------
ponto1.x += 20 * delta_time; //I'M TRYING TO ACCESS AND CHANGE THIS STRUCT
}
我得到的错误:
./src/./globals.h:6:8: error: unknown type name ‘Vector3’
6 | extern Vector3 ponto1;
| ^~~~~~~
In file included from ./src/main.c:6:
./src/./globals.h:6:8: error: unknown type name ‘Vector3’
6 | extern Vector3 ponto1;
| ^~~~~~~
./src/main.c:18:9: error: conflicting types for ‘ponto1’; have ‘Vector3’ {aka ‘struct vector3’}
18 | Vector3 ponto1;
| ^~~~~~
In file included from ./src/main.c:6:
./src/./globals.h:6:16: note: previous declaration of ‘ponto1’ with type ‘int’
6 | extern Vector3 ponto1;
| ^~~~~~
./src/main.c:18:9: error: storage size of ‘ponto1’ isn’t known
18 | Vector3 ponto1;
| ^~~~~~
In file included from ./src/process_input.c:4:
./src/./globals.h:6:8: error: unknown type name ‘Vector3’
6 | extern Vector3 ponto1;
| ^~~~~~~
In file included from ./src/render.c:5:
./src/./globals.h:6:8: error: unknown type name ‘Vector3’
6 | extern Vector3 ponto1;
| ^~~~~~~
./src/render.c: In function ‘render’:
./src/render.c:16:44: error: request for member ‘x’ in something not a structure or union
16 | SDL_RenderDrawPoint(my_renderer, ponto1.x, 50);
| ^
In file included from ./src/setup.c:5:
./src/./globals.h:6:8: error: unknown type name ‘Vector3’
6 | extern Vector3 ponto1;
| ^~~~~~~
./src/setup.c: In function ‘setup’:
./src/setup.c:11:11: error: request for member ‘x’ in something not a structure or union
11 | ponto1.x = 3;
| ^
In file included from ./src/update.c:5:
./src/./globals.h:6:8: error: unknown type name ‘Vector3’
6 | extern Vector3 ponto1;
| ^~~~~~~
./src/update.c: In function ‘update’:
./src/update.c:26:11: error: request for member ‘x’ in something not a structure or union
26 | ponto1.x += 20 * delta_time;
| ^
make: *** [Makefile:2: build] Error 1
您收到错误,因为 globals.h 使用类型
Vector3
但当时尚未定义该类型。
您需要在 globals.h 中
#include "tad.h"
,因为它使用在那里定义的类型。
您看到的另一个问题是因为在包含 tad.h 的文件中并且它使用
Vector3
的字段,该类型引用的结构已被声明但未定义。您需要将结构体定义从 tad.c 移至 tad.h 中,以便其他文件可以使用它。