我正在尝试使用SDL创建一个矩形,但是它不能编译。我以前创建了一个窗口屏幕,它在SDL编译器上工作正常。你可以检查我的代码是否有任何错误。一个例子会有所帮助,谢谢。
#include <SDL.h>
#include <stdio.h>
int main()
{
SDL_Window *p;
SDL_Renderer *w;
p = SDL_CreateWindow("Game",SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,800,640,SDL_WINDOW_SHOWN);
SDL_SetRenderDrawColor(w,255,0,0,255);
SDL_Rect Rect = {220,140,200,200};
SDL_RenderFillRect(w,&Rect);
SDL_Delay(3000);
SDL_DestroyWindow(p);
SDL_Quit();
return 0;
}
编译输出:
1&GT; c:\ c lib \ sdl2-2.0.4 \ sdl2 tutorials \ sdl2 tutorials \ project 2.c(13):error C2275:'SDL_Rect':这种格式的使用方式错误。
1 GT; c:\ c_lib \ sdl2-2.0.4 \ include \ sdl_rect.h(68):请参阅“SDL_Rect”声明。
1 GT; c:\ c_lib \ sdl2-2.0.4 \ sdl2_tutorials \ sdl2_tutorials \ project_2.c(13):错误C2146:语法错误:';' (s)'Rect'不在标识符前面。
1 GT; c:\ c_lib \ sdl2-2.0.4 \ sdl2_tutorials \ sdl2_tutorials \ project_2.c(13):错误C2065:'Rect':未声明的标识符是。
1 GT; c:\ c_lib \ sdl2-2.0.4 \ sdl2_tutorials \ sdl2_tutorials \ project_2.c(13):错误C2059:语法错误:'{'
1 GT; c:\ c_lib \ sdl2-2.0.4 \ sdl2_tutorials \ sdl2_tutorials \ project_2.c(14):错误C2065:'Rect':未声明的标识符是。
1 GT; c:\ c_lib \ sdl2-2.0.4 \ sdl2_tutorials \ sdl2_tutorials \ project_2.c(14):警告C4133:'function':'int *'(on)'const SDL_Rect *'这种格式在not之间不兼容。登记/> ==========构建:0成功,1失败,最新0,0跳过==========
答案 0 :(得分:1)
SDL_Renderer已声明,但尚未初始化并在程序中调用。我修改了你的代码来初始化并调用渲染器,它在我的机器上运行正常。 代码;
#include <SDL.h>
#include <stdio.h>
int main()
{
SDL_Window *p;
SDL_Renderer *w;
p = SDL_CreateWindow("Game",SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,800,640,SDL_WINDOW_SHOWN);
// initialize the renderer
w = SDL_CreateRenderer(p, -1, 0);
// clear the renderer
SDL_RenderClear(w);
SDL_SetRenderDrawColor(w,255,0,0,255);
SDL_Rect Rect = {220,140,200,200};
SDL_RenderFillRect(w,&Rect);
// call the renderer
SDL_RenderPresent(w);
SDL_Delay(3000);
// destroy the renderer
SDL_DestroyRenderer(w);
SDL_DestroyWindow(p);
SDL_Quit();
return 0;
}
希望有所帮助。