如何解决如何使用 SDL2
我正在制作流行益智游戏俄罗斯方块的基于 SDL 的副本。
我开始关注的 tutorial 包含 SDL 中的所有内容,我正在尝试将其移植到 SDL2。
我在教程的帮助下实现了以下内容:
-
Board.h 和 Board.cpp
-
Game.h 和 Game.cpp
-
IO.h
-
Pieces.h 和 Pieces.cpp
本文作者的 IO.cpp 如下:
// ------ Includes -----
#include "IO.h"
static SDL_Surface *mScreen; // Screen
static Uint32 mColors [COLOR_MAX] = {0x000000ff, // Colors
0xff0000ff,0x00ff00ff,0x0000ffff,0x00ffffff,0xff00ffff,0xffff00ff,0xffffffff};
/*
======================================
Init
======================================
*/
IO::IO()
{
InitGraph ();
}
/*
======================================
Clear the screen to black
======================================
*/
void IO::ClearScreen()
{
BoxColor (mScreen,mScreen->w - 1,mScreen->h - 1,mColors[BLACK]);
}
/*
======================================
Draw a rectangle of a given color
Parameters:
>> pX1,pY1: Upper left corner of the rectangle
>> pX2,pY2: Lower right corner of the rectangle
>> pC Rectangle color
======================================
*/
void IO::DrawRectangle (int pX1,int pY1,int pX2,int pY2,enum color pC)
{
BoxColor (mScreen,pX1,pY1,pX2,pY2-1,mColors[pC]);
}
/*
======================================
Return the screen height
======================================
*/
int IO::GetScreenHeight()
{
return mScreen->h;
}
/*
======================================
Update screen
======================================
*/
void IO::UpdateScreen()
{
SDL_Flip(mScreen);
}
/*
======================================
Keyboard Input
======================================
*/
int IO::Pollkey()
{
SDL_Event event;
while ( SDL_PollEvent(&event) )
{
switch (event.type) {
case SDL_KEYDOWN:
return event.key.keysym.sym;
case SDL_QUIT:
exit(3);
}
}
return -1;
}
/*
======================================
Keyboard Input
======================================
*/
int IO::Getkey()
{
SDL_Event event;
while (true)
{
SDL_WaitEvent(&event);
if (event.type == SDL_KEYDOWN)
break;
if (event.type == SDL_QUIT)
exit(3);
};
return event.key.keysym.sym;
}
/*
======================================
Keyboard Input
======================================
*/
int IO::IsKeyDown (int pKey)
{
Uint8* mKeytable;
int mNumkeys;
SDL_PumpEvents();
mKeytable = SDL_GetKeyState(&mNumkeys);
return mKeytable[pKey];
}
/*
======================================
SDL Graphical Initialization
======================================
*/
int IO::InitGraph()
{
const SDL_VideoInfo *info;
Uint8 video_bpp;
Uint32 videoflags;
// Initialize SDL
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr,"Couldn't initialize SDL: %s\n",SDL_GetError());
return 1;
}
atexit(SDL_Quit);
// Alpha blending doesn't work well at 8-bit color
info = SDL_GetVideoInfo();
if ( info->vfmt->BitsPerPixel > 8 ) {
video_bpp = info->vfmt->BitsPerPixel;
} else {
video_bpp = 16;
}
videoflags = SDL_SWSURFACE | SDL_DOUBLEBUF;
// Set 640x480 video mode
if ( (mScreen=SDL_SetVideoMode(640,480,video_bpp,videoflags)) == NULL ) {
fprintf(stderr,"Couldn't set %ix%i video mode: %s\n",640,SDL_GetError());
return 2;
}
return 0;
}
我面临着如何使用 SDL_GetRendererInfo()
实现 InitGraph() 方法的问题而不是 SDL_GetVideoInfo(),因为这些 SDL 方法在 SDL2 中已弃用。
我正在使用 Visual Studio 2019 来构建这个项目。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。