【发布时间】:2013-09-21 19:08:48
【问题描述】:
我有一个项目,我需要使用 SDL 在屏幕上绘制像素。已向我提供了一些代码:
int render_screen(SDL_Surface* screen)
{
pixel pixel_white;
pixel_white.r = (Uint8)0xff;
pixel_white.g = (Uint8)0xff;
pixel_white.b = (Uint8)0xff;
pixel_white.alpha = (Uint8)128;
SDL_LockSurface(screen);
/*do your rendering here*/
/*----------------------*/
SDL_UnlockSurface(screen);
/*flip buffers*/
SDL_Flip(screen);
clear_screen(screen);
return 0;
}
还有这个功能:
void put_pixel(SDL_Surface* screen,int x,int y,pixel* p)
{
Uint32* p_screen = (Uint32*)screen->pixels;
p_screen += y*screen->w+x;
*p_screen = SDL_MapRGBA(screen->format,p->r,p->g,p->b,p->alpha);
}
这段代码中有很多我不明白的地方。首先,我假设我应该从 render_screen 函数中调用函数 put_pixel ,但是使用什么参数? put_pixel(SDL_Surface* screen,int x,int y,pixel* p) 行似乎很复杂。如果 x 和 y 是函数绘制参数,为什么它们会在函数 indata 中声明?我应该使用命令 put_pixel(something,x,y,something2) 调用 put_pixel。如果我使用 x=56 和 y=567,当在 parentesis 中声明时它们不是重置为 0 吗?我应该添加什么东西来让它工作?
【问题讨论】:
-
我认为这里真正的问题是你是 C 的初学者。C 一开始是一门具有挑战性的语言。如果您想开始编写游戏,您可能会使用 PyGame 或 Löve 获得更大的成功。