gpt4 book ai didi

使用 SDL 在 C 上创建矩形

转载 作者:行者123 更新时间:2023-11-30 21:01:40 25 4
gpt4 key购买 nike

我正在尝试使用 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> c: \ c lib \ sdl2-2.0.4 \ sdl2 tutorials \ sdl2 tutorials \ project 2.c (13): error C2275: 'SDL_Rect': this format was used in the wrong way.
1> c: \ c_lib \ sdl2-2.0.4 \ include \ sdl_rect.h (68): Refer to the 'SDL_Rect' declarations.
1> c: \ c_lib \ sdl2-2.0.4 \ sdl2_tutorials \ sdl2_tutorials \ project_2.c (13): error C2146: syntax error: ';' (s) 'Rect' not in front of the identifier.
1> c: \ c_lib \ sdl2-2.0.4 \ sdl2_tutorials \ sdl2_tutorials \ project_2.c (13): error C2065: 'Rect': undeclared identifier is.
1> c: \ c_lib \ sdl2-2.0.4 \ sdl2_tutorials \ sdl2_tutorials \ project_2.c (13): error C2059: syntax error: '{'
1> c: \ c_lib \ sdl2-2.0.4 \ sdl2_tutorials \ sdl2_tutorials \ project_2.c (14): error C2065: 'Rect': undeclared identifier is.
1> c: \ c_lib \ sdl2-2.0.4 \ sdl2_tutorials \ sdl2_tutorials \ project_2.c (14): warning C4133: 'function': 'int *' (on) 'const SDL_Rect *' This format is not compatible between not.
========== Build: 0 succeeded, 1 failed, the latest 0, 0 skipped ==========

最佳答案

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;
}

希望有帮助。

关于使用 SDL 在 C 上创建矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34988132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com