- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我尝试编写以下类(class)时 http://lazyfoo.net/downloads/index.php?file=SDLTut_lesson19
我在 Visual Studio 2010 中有这个输出:
1>------ Build started: Project: 19-circularcollisiondetect, Configuration: Debug Win32 ------
1>Build started 02/03/2012 03:47:51.
1>InitializeBuildStatus:
1> Touching "Debug\19-circularcollisiondetect.unsuccessfulbuild".
1>ClCompile:
1> main.cpp
1>c:\users\fab\desktop\c++\sdl 1.2.15\19-circularcollisiondetect\19-circularcollisiondetect\main.cpp(140): error C2668: 'pow' : ambiguous call to overloaded function
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\math.h(583): could be 'long double pow(long double,int)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\math.h(535): or 'float pow(float,int)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\math.h(497): or 'double pow(double,int)'
1> while trying to match the argument list '(int, int)'
1>c:\users\fab\desktop\c++\sdl 1.2.15\19-circularcollisiondetect\19-circularcollisiondetect\main.cpp(140): error C2668: 'pow' : ambiguous call to overloaded function
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\math.h(583): could be 'long double pow(long double,int)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\math.h(535): or 'float pow(float,int)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\math.h(497): or 'double pow(double,int)'
1> while trying to match the argument list '(int, int)'
1>c:\users\fab\desktop\c++\sdl 1.2.15\19-circularcollisiondetect\19-circularcollisiondetect\main.cpp(162): warning C4018: '<' : signed/unsigned mismatch
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.80
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
我该如何解决?我正在使用 Visual Studio 2010 和 SDL 1.2.15。我没有修改原始代码。你能验证一下你是否可以编译它吗?
/*This source code copyrighted by Lazy Foo' Productions (2004-2012)
and may not be redestributed without written permission.*/
//The headers
#include "SDL.h"
#include "SDL_image.h"
#include <string>
#include <vector>
#include <cmath>
//Screen attributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
//The frame rate
const int FRAMES_PER_SECOND = 20;
//The dot attributes
const int DOT_WIDTH = 20;
//The surfaces
SDL_Surface *dot = NULL;
SDL_Surface *screen = NULL;
//The event structure
SDL_Event event;
//A circle structure
struct Circle
{
int x, y;
int r;
};
//The dot
class Dot
{
private:
//The area of the dot
Circle c;
//The velocity of the dot
int xVel, yVel;
public:
//Initializes the variables
Dot();
//Takes key presses and adjusts the dot's velocity
void handle_input();
//Moves the dot
void move( std::vector<SDL_Rect> &rects, Circle &circle );
//Shows the dot on the screen
void show();
};
//The timer class
class Timer
{
private:
//The clock time when the timer started
int startTicks;
//The ticks stored when the timer was paused
int pausedTicks;
//The timer status
bool paused;
bool started;
public:
//Initializes variables
Timer();
//The various clock actions
void start();
void stop();
void pause();
void unpause();
//Gets the timer's time
int get_ticks();
//Checks the status of the timer
bool is_started();
bool is_paused();
};
SDL_Surface *load_image( std::string filename )
{
//The image that's loaded
SDL_Surface* loadedImage = NULL;
//The optimized surface that will be used
SDL_Surface* optimizedImage = NULL;
//Load the image
loadedImage = IMG_Load( filename.c_str() );
//If the image loaded
if( loadedImage != NULL )
{
//Create an optimized surface
optimizedImage = SDL_DisplayFormat( loadedImage );
//Free the old surface
SDL_FreeSurface( loadedImage );
//If the surface was optimized
if( optimizedImage != NULL )
{
//Color key surface
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
}
}
//Return the optimized surface
return optimizedImage;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
//Holds offsets
SDL_Rect offset;
//Get offsets
offset.x = x;
offset.y = y;
//Blit
SDL_BlitSurface( source, clip, destination, &offset );
}
double distance( int x1, int y1, int x2, int y2 )
{
//Return the distance between the two points
return sqrt( pow( x2 - x1, 2 ) + pow( y2 - y1, 2 ) );
}
bool check_collision( Circle &A, Circle &B )
{
//If the distance between the centers of the circles is less than the sum of their radii
if( distance( A.x, A.y, B.x, B.y ) < ( A.r + B.r ) )
{
//The circles have collided
return true;
}
//If not
return false;
}
bool check_collision( Circle &A, std::vector<SDL_Rect> &B )
{
//Closest point on collision box
int cX, cY;
//Go through the B boxes
for( int Bbox = 0; Bbox < B.size(); Bbox++ )
{
//Find closest x offset
if( A.x < B[ Bbox ].x )
{
cX = B[ Bbox ].x;
}
else if( A.x > B[ Bbox ].x + B[ Bbox ].w )
{
cX = B[ Bbox ].x + B[ Bbox ].w;
}
else
{
cX = A.x;
}
//Find closest y offset
if( A.y < B[ Bbox ].y )
{
cY = B[ Bbox ].y;
}
else if( A.y > B[ Bbox ].y + B[ Bbox ].h )
{
cY = B[ Bbox ].y + B[ Bbox ].h;
}
else
{
cY = A.y;
}
//If the closest point is inside the circle
if( distance( A.x, A.y, cX, cY ) < A.r )
{
//This box and the circle have collided
return true;
}
}
//If the shapes have not collided
return false;
}
bool init()
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return false;
}
//Set up the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
//If there was an error in setting up the screen
if( screen == NULL )
{
return false;
}
//Set the window caption
SDL_WM_SetCaption( "Move the Dot", NULL );
//If everything initialized fine
return true;
}
bool load_files()
{
//Load the dot image
dot = load_image( "dot.bmp" );
//If there was a problem in loading the dot
if( dot == NULL )
{
return false;
}
//If everything loaded fine
return true;
}
void clean_up()
{
//Free the surface
SDL_FreeSurface( dot );
//Quit SDL
SDL_Quit();
}
Dot::Dot()
{
//Initialize the offsets and dimentions
c.x = DOT_WIDTH / 2;
c.y = DOT_WIDTH / 2;
c.r = DOT_WIDTH / 2;
//Initialize the velocity
xVel = 0;
yVel = 0;
}
void Dot::handle_input()
{
//If a key was pressed
if( event.type == SDL_KEYDOWN )
{
//Adjust the velocity
switch( event.key.keysym.sym )
{
case SDLK_UP: yVel -= 1; break;
case SDLK_DOWN: yVel += 1; break;
case SDLK_LEFT: xVel -= 1; break;
case SDLK_RIGHT: xVel += 1; break;
}
}
//If a key was released
else if( event.type == SDL_KEYUP )
{
//Adjust the velocity
switch( event.key.keysym.sym )
{
case SDLK_UP: yVel += 1; break;
case SDLK_DOWN: yVel -= 1; break;
case SDLK_LEFT: xVel += 1; break;
case SDLK_RIGHT: xVel -= 1; break;
}
}
}
void Dot::move( std::vector<SDL_Rect> &rects, Circle &circle )
{
//Move the dot left or right
c.x += xVel;
//If the dot went too far to the left or right or has collided with the other shapes
if( ( c.x - DOT_WIDTH / 2 < 0 ) || ( c.x + DOT_WIDTH / 2 > SCREEN_WIDTH ) || ( check_collision( c, rects ) ) || ( check_collision( c, circle ) ) )
{
//Move back
c.x -= xVel;
}
//Move the dot up or down
c.y += yVel;
//If the dot went too far up or down or has collided with the other shapes
if( ( c.y - DOT_WIDTH / 2 < 0 ) || ( c.y + DOT_WIDTH / 2 > SCREEN_HEIGHT ) || ( check_collision( c, rects ) ) || ( check_collision( c, circle ) ) )
{
//Move back
c.y -= yVel;
}
}
void Dot::show()
{
//Show the dot
apply_surface( c.x - c.r, c.y - c.r, dot, screen );
}
Timer::Timer()
{
//Initialize the variables
startTicks = 0;
pausedTicks = 0;
paused = false;
started = false;
}
void Timer::start()
{
//Start the timer
started = true;
//Unpause the timer
paused = false;
//Get the current clock time
startTicks = SDL_GetTicks();
}
void Timer::stop()
{
//Stop the timer
started = false;
//Unpause the timer
paused = false;
}
void Timer::pause()
{
//If the timer is running and isn't already paused
if( ( started == true ) && ( paused == false ) )
{
//Pause the timer
paused = true;
//Calculate the paused ticks
pausedTicks = SDL_GetTicks() - startTicks;
}
}
void Timer::unpause()
{
//If the timer is paused
if( paused == true )
{
//Unpause the timer
paused = false;
//Reset the starting ticks
startTicks = SDL_GetTicks() - pausedTicks;
//Reset the paused ticks
pausedTicks = 0;
}
}
int Timer::get_ticks()
{
//If the timer is running
if( started == true )
{
//If the timer is paused
if( paused == true )
{
//Return the number of ticks when the timer was paused
return pausedTicks;
}
else
{
//Return the current time minus the start time
return SDL_GetTicks() - startTicks;
}
}
//If the timer isn't running
return 0;
}
bool Timer::is_started()
{
return started;
}
bool Timer::is_paused()
{
return paused;
}
int main( int argc, char* args[] )
{
//Quit flag
bool quit = false;
//Make the dot
Dot myDot;
//Make the shapes
std::vector<SDL_Rect> box( 1 );
Circle otherDot;
//Set the shapes' attributes
box[ 0 ].x = 60;
box[ 0 ].y = 60;
box[ 0 ].w = 40;
box[ 0 ].h = 40;
otherDot.x = 30;
otherDot.y = 30;
otherDot.r = DOT_WIDTH / 2;
//The frame rate regulator
Timer fps;
//Initialize
if( init() == false )
{
return 1;
}
//Load the files
if( load_files() == false )
{
return 1;
}
//While the user hasn't quit
while( quit == false )
{
//Start the frame timer
fps.start();
//While there's events to handle
while( SDL_PollEvent( &event ) )
{
//Handle events for the dot
myDot.handle_input();
//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
}
//Move the dot
myDot.move( box, otherDot );
//Fill the screen white
SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
//Show the box
SDL_FillRect( screen, &box[ 0 ], SDL_MapRGB( screen->format, 0x00, 0x00, 0x00 ) );
//Show the other dot
apply_surface( otherDot.x - otherDot.r, otherDot.y - otherDot.r, dot, screen );
//Show our dot
myDot.show();
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
//Cap the frame rate
if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
{
SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
}
}
//Clean up
clean_up();
return 0;
}
最佳答案
如错误所示:
1>c:\users\fab\desktop\c++\sdl 1.2.15\19-circularcollisiondetect\19-circularcollisiondetect\main.cpp(140): error C2668: 'pow' : ambiguous call to overloaded function
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\math.h(583): could be 'long double pow(long double,int)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\math.h(535): or 'float pow(float,int)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\math.h(497): or 'double pow(double,int)'
您将两个整数传递给 pow(),但它需要一个 long double 和 int、float 和 int,或者 double 和 int。类型转换其中之一应该为您解决这个问题,例如:
return sqrt( pow((double)(x2 - x1), 2 ) + pow((double)(y2 - y1), 2 ) );
我在这里使用了 double ,因为它比 float 更准确地表示整数的整个范围而不会过分,但是如果你处理的是小数字(在 100 和 1000 之间),你可能只使用 float .
关于c++ - 无法编译此 SDL 类(class)... :/,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9527749/
问题解决了 转到底部查看解决方案。 大家好,我用谷歌搜索了很长时间,但没有找到任何东西,所以,我们开始吧: 我有这样的设置: Compiler: MinGW IDE: Netbeans Library
如何在 SDL 中创建一个插入主窗口的新窗口?所以它可以是有重点的,有单独的绘图上下文和单独的处理事件。 最佳答案 窗中窗 您可以使用下面的示例在窗口中创建一个窗口。该示例将生成两个窗口,其中 sub
我是 SDL 的新手,我只是好奇为什么 sdl 使用静态和动态库?我的意思是,sdl.dll 中有哪些函数,为什么它是动态链接而不是静态链接?谢谢。 最佳答案 SDL.dll包含您在 SDL 中使用的
我通过 brew 在我的 mac 上安装了 SDL,但我无法包含它!这是我太简单的代码: #include int main(){ return 0; } 用cc编译时,CC找不到SDL.h
我正在尝试制作一个以恒定帧速率运行的 SDL 程序。但是我发现即使我的程序滞后很多并且跳过了很多帧(即使它以低帧运行并且没有渲染太多)。 你们有什么建议可以让我的程序运行更流畅吗? #include
我目前正在编写一款非常复杂和狡猾的游戏,它会让您充满敬畏和胜利 - 哦,好吧,这是 15 个谜题,我只是在熟悉 SDL。 我在窗口模式下运行,并使用 SDL_Flip 作为一般情况的页面更新,因为它会
我知道 SDL_TEXTUREACCESS_STATIC 和 SDL_TEXTUREACCESS_TARGET 之间的区别。我正在为一款游戏编写 SDL 后端,该游戏的平台无关核心不会告诉平台相关纹理
我们最近移植了 Bitfighter从 GLUT 到 SDL。这样做有很多好处,但也有一些缺点,特别是在窗口管理领域。 Bitfighter 在固定纵横比窗口(800x600 像素)中运行。用户可以将
我正在尝试使用 SDL 2.0 了解整个 2D 加速渲染过程。 所以我的问题是在屏幕上绘制圆圈最有效的方法是什么,为什么? 一些方法是: 首先创建一个软件表面,然后在该表面上绘制必要的像素,然后从该表
我正在尝试将纹理保存到 png 中,而我唯一得到的是 是屏幕的一部分的屏幕截图。 我的代码示例: src_texture = SDL_CreateTextureFromSurface( render
我正在使用 SDL1.2 在我的 openGL 框架中处理窗口管理。 程序运行时是否可以在不调用 SDL_Quit() 的情况下破坏窗口(表面)? ? 背景:我的框架实际上只是多媒体环境中的一个 gf
如何在 SDL 中缩放 Sprite ? 最佳答案 SDL 不直接提供缩放功能,但有 an additional library called SDL_gfx它提供旋转和缩放功能。还有another
我刚刚开始使用SDL2_ttf。我已经弄清楚如何使用 TTF_RenderText_Blished 在屏幕上获取一些文本,但是如何让它进行换行和自动换行? 似乎不支持\n;它只是创造一个空间而不是沿着
我安装了 FEDORA 和 SDL,并希望在编译时用 C 编程图形,我得到了很多对 SDL_MapRGB、SDL_Init 等的 undefined reference 我搜索了文件系统 SDL.dl
我在我的项目中使用 SDL 库,并且在 Windows 平台上工作。 当我决定将项目迁移到 SDL 2 时,我遇到了一个问题: SDL 1.2 中有一个选项可以将 stdout/stderr 的输出打
我正在尝试使用 Derelict 和 D 编写一个简单的图形测试程序。 当我尝试用 SDL 做几乎任何事情时,它都会出现段错误。这是有问题的代码: import std.stdio; import
我在考虑是否尝试使用 SDL 作为 DirectX 的替代品,似乎我只创建 2D 游戏,但是我找不到任何地方是否需要在最终用户计算机上安装 SDL 才能玩用 C# 开发的游戏,图形使用 SDL。 有人
我想使用 SDL_net 作为一个供少数程序使用的帮助程序库。然而,其中一些程序本身可能已经在使用 SDL。如果我理解正确的话,这意味着我在初始化/释放我的库时不能盲目使用 SDL_Init 和 SD
我希望我的游戏引擎停止将鼠标移动到中心(用于偏航和俯仰相机计算)。我写了一些应该处理它的代码,但鼠标在最小化时仍然移动。 void mainLoop() { // This is the ma
结构SDL_Keysym 有SDL_Scancode 和SDL_Keycode 成员。它们之间有什么区别?该文档并没有真正为我清除它。我都试过了,它们似乎做同样的事情。 最佳答案 参见 the SDL
我是一名优秀的程序员,十分优秀!