Draw sprite from offset into tilesheet

This commit is contained in:
Doyle Thai 2016-06-18 20:45:14 +10:00
parent 753d700ca6
commit bb57f080c9
5 changed files with 45 additions and 24 deletions

Binary file not shown.

View File

@ -57,11 +57,6 @@ Texture genTexture(const GLuint width, const GLuint height,
glTexImage2D(GL_TEXTURE_2D, 0, tex.internalFormat, tex.width, tex.height, 0, glTexImage2D(GL_TEXTURE_2D, 0, tex.internalFormat, tex.width, tex.height, 0,
tex.imageFormat, GL_UNSIGNED_BYTE, image); tex.imageFormat, GL_UNSIGNED_BYTE, image);
glCheckError(); glCheckError();
glActiveTexture(GL_TEXTURE0);
// TODO(doyle): Don't forget about thsi!
glTexSubImage2D(GL_TEXTURE_2D, 0, 3, 3, 55, 55, GL_RGBA, GL_UNSIGNED_BYTE,
image);
glCheckError();
glGenerateMipmap(GL_TEXTURE_2D); glGenerateMipmap(GL_TEXTURE_2D);
/* Set parameter of currently bound texture */ /* Set parameter of currently bound texture */

View File

@ -6,7 +6,7 @@ void worldTraveller_gameInit(GameState *state)
{ {
/* Initialise assets */ /* Initialise assets */
asset_loadTextureImage( asset_loadTextureImage(
"data/textures/WorldTraveller/elisa-spritesheet1.png", texlist_hero); "data/textures/WorldTraveller/TerraSprite.png", texlist_hero);
glCheckError(); glCheckError();
state->state = state_active; state->state = state_active;
@ -23,15 +23,48 @@ void worldTraveller_gameInit(GameState *state)
shader_uniformSetMat4fv(renderer->shader, "projection", projection); shader_uniformSetMat4fv(renderer->shader, "projection", projection);
glCheckError(); glCheckError();
/* Init hero */
Entity *hero = &state->hero;
hero->tex = asset_getTexture(texlist_hero);
hero->size = V2(91.0f, 146.0f);
//v2 screenCentre = V2(state->width / 2.0f, state->height / 2.0f);
//v2 heroOffsetToCentre =
// V2(-(hero->size.x / 2.0f), -(hero->size.y / 2.0f));
//v2 heroCentered = v2_add(screenCentre, heroOffsetToCentre);
hero->pos = V2(0.0f, 0.0f);
glCheckError();
Texture *heroSheet = asset_getTexture(texlist_hero);
v2 sheetSize = V2(CAST(f32)heroSheet->width, CAST(f32)heroSheet->height);
if (sheetSize.x != sheetSize.y)
{
printf(
"worldTraveller_gameInit() warning: Sprite sheet is not square: "
"%dx%dpx\n",
CAST(i32) sheetSize.x, CAST(i32) sheetSize.y);
}
f32 uvNormalisedFactor = sheetSize.x;
v2 heroStartPixel = V2(219.0f, 14.0f);
v4 heroRect =
V4(heroStartPixel.x, heroStartPixel.y, heroStartPixel.x + hero->size.x,
heroStartPixel.y + hero->size.y);
v4 heroUVNormalised = v4_scale(heroRect, 1.0f/uvNormalisedFactor);
heroUVNormalised.y = 1.0f - heroUVNormalised.y;
heroUVNormalised.w = 1.0f - heroUVNormalised.w;
/* Init renderer */ /* Init renderer */
// NOTE(doyle): Draws a series of triangles (three-sided polygons) using // NOTE(doyle): Draws a series of triangles (three-sided polygons) using
// vertices v0, v1, v2, then v2, v1, v3 (note the order) // vertices v0, v1, v2, then v2, v1, v3 (note the order)
v4 vertices[] = { v4 vertices[] = {
// x y s t // x y s t
{0.0f, 1.0f, 0.0f, 1.0f}, // Top left {0.0f, 1.0f, heroUVNormalised.x, heroUVNormalised.y}, // Top left
{0.0f, 0.0f, 0.0f, 0.0f}, // Bottom left {0.0f, 0.0f, heroUVNormalised.x, heroUVNormalised.w}, // Bottom left
{1.0f, 1.0f, 1.0f, 1.0f}, // Top right {1.0f, 1.0f, heroUVNormalised.z, heroUVNormalised.y}, // Top right
{1.0f, 0.0f, 1.0f, 0.0f}, // Bottom right {1.0f, 0.0f, heroUVNormalised.z, heroUVNormalised.w}, // Bottom right
}; };
GLuint VBO; GLuint VBO;
@ -60,20 +93,6 @@ void worldTraveller_gameInit(GameState *state)
/* Unbind */ /* Unbind */
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0); glBindVertexArray(0);
glCheckError();
/* Init hero */
Entity *hero = &state->hero;
hero->tex = asset_getTexture(texlist_hero);
hero->size = V2(260.0f, 260.0f);
//v2 screenCentre = V2(state->width / 2.0f, state->height / 2.0f);
//v2 heroOffsetToCentre =
// V2(-(hero->size.x / 2.0f), -(hero->size.y / 2.0f));
//v2 heroCentered = v2_add(screenCentre, heroOffsetToCentre);
hero->pos = V2(0.0f, 0.0f);
glCheckError(); glCheckError();
} }

View File

@ -102,6 +102,13 @@ INTERNAL inline f32 v##num##_dot(const v##num a, const v##num b) \
for (i32 i = 0; i < ##num; i++) { result += (a.e[i] * b.e[i]); } \ for (i32 i = 0; i < ##num; i++) { result += (a.e[i] * b.e[i]); } \
return result; \ return result; \
} \ } \
INTERNAL inline b32 v##num##_equals(const v##num a, const v##num b) \
{ \
b32 result = TRUE; \
for (i32 i = 0; i < ##num; i++) \
if (a.e[i] != b.e[i]) result = FALSE; \
return result; \
} \
DEFINE_VECTOR_MATH(2); DEFINE_VECTOR_MATH(2);
DEFINE_VECTOR_MATH(3); DEFINE_VECTOR_MATH(3);