C++, OpenGL, SDL2 - Анимация
Sprite.cpp
#include "Sprite.hpp"
using namespace fox;
Sprite::Sprite(){
sprite.setPointCount(4);
}
void Sprite::setSize(float width, float height){
size.width=width; size.height=height;
sprite.setPointSize(1, width, 0);
sprite.setPointSize(2, width, height);
sprite.setPointSize(3, 0, height);
}
void Sprite::setPosition(float x, float y){
position.x=x; position.y=y;
}
void Sprite::loadTexture(std::string path){
texturePath=path;
willLoadTexture=true;
}
void Sprite::show(){
if(willLoadTexture){
glDeleteTextures(1, &textureID);
texture=SDL_LoadBMP(texturePath.c_str());
texture = SDL_ConvertSurfaceFormat(texture,
SDL_PIXELFORMAT_ABGR8888, 0);
if(texture->format->BitsPerPixel>=4)
mode=GL_RGBA;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, mode, texture->w, texture->h,
0, mode, GL_UNSIGNED_BYTE, texture->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
size.width=texture->w;
size.height=texture->h;
textureSize.width=size.width;
textureSize.height=size.height;
}
willLoadTexture=false;
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glTranslatef(getPosition().x, getPosition().y, 0);
glRotatef(anglee, 0, 0, 1);
glTranslatef(-getPosition().x, -getPosition().y, 0);
glTranslatef(position.x, position.y, 0);
glBegin(GL_POLYGON);
glTexCoord2f(0, 0); sprite.getPoint(0);
glTexCoord2f(1, 0); sprite.getPoint(1);
glTexCoord2f(1, 1); sprite.getPoint(2);
glTexCoord2f(0, 1); sprite.getPoint(3);
glEnd();
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
}
void Sprite::setScale(float x, float y){
setSize((float)getSize().width*x,
(float)getSize().height*y);
}
void Sprite::move(float x, float y){
setPosition((float)getPosition().x+x,
(float)getPosition().y+y);
}
void Sprite::setRotation(float angle){
anglee=angle;
}
void Sprite::rotate(float angle){
anglee+=angle;
}
Vector2f Sprite::getPosition(){
return position;
}
Rectf Sprite::getSize(){
return size;
}
Rectf Sprite::getTextureSize(){
return textureSize;
}
Sprite.hpp:
#ifndef FOX_SPRITE_HPP
#define FOX_SPRITE_HPP
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
#include "System/Vector2.hpp"
#include "System/Rect.hpp"
#include "Polygon.hpp"
#include <string>
namespace fox{
class Sprite{
public:
Sprite();
void setSize(float width, float height);
void setPosition(float x, float y);
void loadTexture(std::string path);
void show();
void setScale(float x, float y);
void move(float x, float y);
void setRotation(float angle);
void rotate(float angle);
Vector2f getPosition();
Rectf getSize();
Rectf getTextureSize();
private:
bool willLoadTexture=false;
std::string texturePath;
float anglee=0;
int mode=GL_RGB, param=GL_NEAREST;
GLuint textureID=0;
SDL_Surface *texture;
Vector2f position;
Rectf size;
Rectf textureSize;
fox::Polygon sprite;
};
}
#endif // FOX_SPRITE_HPP
Написал класс спрайта на языке OpenGL! Сделал анимацию двух текстур, но через 30 секунд после запуска окна, окно вылетает!
Без анимаций всё идеально работает!
Для захвата миллисекундов использую SDL_GetTicks();!
Как исправить?