使用 SDL2 渲染精灵
Rendering Sprites with SDL2
我对游戏开发非常陌生。我正在尝试使用 SDL 在窗口上移动精灵。我使用 http://gamedevgeek.com/tutorials/moving-sprites-with-sdl/作为参考,以帮助我了解 SDL。但是,这种 blit 方法不适用于 SDL2。我研究并发现我必须将表面转换为纹理并渲染它们,但我遇到了一些令人沮丧的困难。运行时,背景图像似乎渲染得很好,但精灵只出现在窗口的一角,移动时,似乎被背景覆盖了。这是代码:
#include <iostream>
#include "stdafx.h"
#include <SDL.h>
#include <SDL_image.h>
const int WIDTH = 900;
const int HEIGHT = 360;
const int SPRITE_SIZE = 256;
int main(int argc, char *argv[])
{
SDL_Surface *imageSurface = NULL;
SDL_Surface *windowSurface = NULL;
SDL_Surface *temp = NULL;
SDL_Surface *sprite = NULL;
SDL_Surface *SDL_DisplayFormat(SDL_Surface *surface);
SDL_Rect rcSprite;
SDL_Rect gdSprite;
SDL_Event windowEvent;
SDL_Event event;
SDL_Renderer *renderer = NULL;
SDL_Texture *texture;
SDL_Texture *spriteTexture;
const Uint8 *keystate;
int colorkey;
int count;
int xPosition = 0;
int yPosition = 0;
int gameover = 0;
SDL_Window *window = SDL_CreateWindow("ABDUCTO", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_ALLOW_HIGHDPI);
windowSurface = SDL_GetWindowSurface(window);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
imageSurface = IMG_Load("farm.png");
sprite= IMG_Load("sprite6.png");
texture = SDL_CreateTextureFromSurface(renderer, imageSurface);
spriteTexture = SDL_CreateTextureFromSurface(renderer, sprite);
SDL_FreeSurface(sprite);
SDL_FreeSurface(imageSurface);
//rcSprite used as source rectangle, gdSprite as destination rectangle. Initialize them to the same position
rcSprite.x = xPosition;
rcSprite.y = yPosition;
rcSprite.w = SPRITE_SIZE;
rcSprite.h = SPRITE_SIZE;
gdSprite.x = xPosition;
gdSprite.y = yPosition;
gdSprite.w = SPRITE_SIZE;
gdSprite.h = SPRITE_SIZE;
while (!gameover)
{
if (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
gameover = 1;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
case SDLK_q:
gameover = 1;
break;
}
break;
}
}
keystate = SDL_GetKeyboardState(NULL);
// When key pressed, update the destination rectangle
if (keystate[SDL_SCANCODE_LEFT]) {
gdSprite.x -= 2;
}
if (keystate[SDL_SCANCODE_RIGHT]) {
gdSprite.x += 2;
}
if (keystate[SDL_SCANCODE_UP]) {
gdSprite.y -= 2;
}
if (keystate[SDL_SCANCODE_DOWN]) {
gdSprite.y += 2;
}
if (gdSprite.x < 0) {
gdSprite.x = 0;
}
else if (gdSprite.x > WIDTH - SPRITE_SIZE) {
gdSprite.x = WIDTH - SPRITE_SIZE;
}
if(gdSprite.y < 0) {
gdSprite.y = 0;
}
else if (gdSprite.y > HEIGHT - SPRITE_SIZE) {
gdSprite.y = HEIGHT - SPRITE_SIZE;
}
//Render the window
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderCopy(renderer, spriteTexture, &rcSprite, &gdSprite);
SDL_RenderPresent(renderer);
//SDL_BlitSurface(imageSurface, NULL, windowSurface, NULL);
//SDL_BlitSurface(sprite, NULL, imageSurface, &rcSprite);
//SDL_UpdateWindowSurface(window);
//update the source rectangle to move with the sprite??
rcSprite.x = gdSprite.x;
rcSprite.y = gdSprite.y;
}
SDL_DestroyTexture(spriteTexture);
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
return 0;
SDL_Quit();
}
任何意见都值得赞赏。谢谢。
可能必须对图像的路径执行某些操作,或者在编译时将文件复制到资源中。您使用的是Windows,OSX还是Linux?您使用的是什么 IDE?
但我注意到两件事:
1(在SDL_CreateWindow
之前,您应该初始化 SDL: SDL_Init(SDL_INIT_EVERYTHING);
2( SDL_Quit();
永远不会被调用,因为上面的一行退出了主函数,return 0;
=>你应该交换线条!
注意到更多:3(不要update the source rectangle to move with the sprite
只需将整个精灵渲染到 gdSprite 位置: SDL_RenderCopy(renderer, sTexture, NULL, &gdSprite);
相关文章:
- 如何创建事件驱动的 SDL2 应用程序
- 使用 SDL2 和SDL_image C++加载图像
- 将 SDL2 与 CMake 链接起来
- SDL2 透明背景
- OpenGL 着色器不绘制任何东西 - 糟糕的精灵类
- 如何在使用 SDL2 的 Eclipse 中构建 c++ 程序
- 不带轮询的 SDL2 事件回调
- C++ SDL2:如何将矩形渲染到多个视口中
- SDL2 调整窗口大小后如何缩放鼠标坐标?
- SDL2 二维纹理分配/池化
- 从精灵表在 sfml 中为精灵制作动画
- 如何使用 sdl2 快速绘制像素网格?
- 看不到文件'SDL2.lib'
- 我的 SDL2 程序需要哪些二进制文件,以便它在另一台未安装 SDL2 的计算机中工作
- SDL2 无法正确绘制矩形
- 使用 SDL2 渲染精灵
- 用C++在SDL2.0中渲染来自同一纹理的多个精灵
- SDL2 c++捕捉渲染动画/精灵的视频
- SDL2.0移动精灵与键盘
- 我如何在使用SDL2获取一组所有输入后显示精灵的运动?