123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- /*!
- * \file src/Sprite.cpp
- * \brief World Sprite
- *
- * \author xythobuz
- */
-
- #include "glm/glm.hpp"
-
- #include "global.h"
- #include "Camera.h"
- #include "Game.h"
- #include "Render.h"
- #include "TextureManager.h"
- #include "Sprite.h"
-
- SpriteSequence::SpriteSequence(int32_t objectID) {
- id = objectID;
- }
-
- void SpriteSequence::add(Sprite s) {
- sprites.push_back(s);
- }
-
- unsigned long SpriteSequence::size() {
- return sprites.size();
- }
-
- Sprite& SpriteSequence::get(unsigned long index) {
- assert(index < sprites.size());
- return sprites.at(index);
- }
-
- // ----------------------------------------------------------------------------
-
- Sprite::Sprite(uint16_t tile, uint8_t x, uint8_t y, uint16_t width, uint16_t height) {
- /*!
- * \fixme TODO Can't do translation/visibility-check when rendering here.
- * We don't know xyz of this Sprite because it could be used by
- * different items at other coordinates in the world.
- * Do translation/visibility-check at item/room level.
- */
-
- const float scale = 4.0f;
- const float texelScale = 256.0f;
-
- width >>= 8;
- height >>= 8;
- int width2 = (int)(width * scale);
- int height2 = (int)(height * scale);
-
- vertex[0][0] = -width2 / 2.0f;
- vertex[1][0] = -width2 / 2.0f;
- vertex[2][0] = width2 / 2.0f;
- vertex[3][0] = width2 / 2.0f;
-
- vertex[0][1] = 0;
- vertex[1][1] = -height2;
- vertex[2][1] = -height2;
- vertex[3][1] = 0;
-
- vertex[0][2] = 0;
- vertex[1][2] = 0;
- vertex[2][2] = 0;
- vertex[3][2] = 0;
-
- texel[3][0] = (float)(x + width) / texelScale;
- texel[3][1] = (float)(y + height) / texelScale;
-
- texel[2][0] = (float)(x + width) / texelScale;
- texel[2][1] = (float)(y) / texelScale;
-
- texel[1][0] = (float)(x) / texelScale;
- texel[1][1] = (float)(y) / texelScale;
-
- texel[0][0] = (float)(x) / texelScale;
- texel[0][1] = (float)(y + height) / texelScale;
-
- texture = tile;
- //radius = width2 / 2.0f;
- }
-
- void Sprite::display() {
- /*
- //if (!Render::isVisible(pos[0], pos[1], pos[2], radius))
- // return;
-
- glPushMatrix();
- //glTranslated(pos[0], pos[1], pos[2]);
-
- // Sprites must always face camera, because they have no depth =)
- glRotated(glm::degrees(Camera::getRadianYaw()), 0, 1, 0);
-
- switch (Render::getMode()) {
- // No vertex lighting on sprites, as far as I see in specs
- // So just draw normal texture, no case 2
- case RenderMode::Solid:
- glBegin(GL_TRIANGLE_STRIP);
- glColor3f(texel[0][0], texel[0][1], 0.5);
- glVertex3fv(vertex[0]);
-
- glColor3f(texel[1][0], texel[1][1], 0.5);
- glVertex3fv(vertex[1]);
-
- glColor3f(texel[3][0], texel[3][1], 0.5);
- glVertex3fv(vertex[3]);
-
- glColor3f(texel[2][0], texel[2][1], 0.5);
- glVertex3fv(vertex[2]);
- glEnd();
- break;
- case RenderMode::Wireframe:
- glColor3ubv(CYAN);
- glBegin(GL_LINE_LOOP);
- glVertex3fv(vertex[0]);
- glVertex3fv(vertex[1]);
- glVertex3fv(vertex[2]);
- glVertex3fv(vertex[3]);
- glEnd();
- glColor3ubv(WHITE);
- break;
- default:
- getTextureManager().bindTextureId(texture);
-
- glBegin(GL_TRIANGLE_STRIP);
- glTexCoord2fv(texel[0]);
- glVertex3fv(vertex[0]);
- glTexCoord2fv(texel[1]);
- glVertex3fv(vertex[1]);
- glTexCoord2fv(texel[3]);
- glVertex3fv(vertex[3]);
- glTexCoord2fv(texel[2]);
- glVertex3fv(vertex[2]);
- glEnd();
- }
-
- glPopMatrix();
- */
- }
-
- void Sprite::display(float x, float y, float w, float h) {
- /*
- float z = 0.0f;
-
- getTextureManager().bindTextureId(texture);
-
- glBegin(GL_QUADS);
-
- glTexCoord2fv(texel[1]);
- glVertex3f(x, y, z);
-
- glTexCoord2fv(texel[2]);
- glVertex3f(x + w, y, z);
-
- glTexCoord2fv(texel[3]);
- glVertex3f(x + w, y + h, z);
-
- glTexCoord2fv(texel[0]);
- glVertex3f(x, y + h, z);
-
- glEnd();
- */
- }
|