summaryrefslogtreecommitdiff
path: root/src/background.c
blob: 7929fa1dde29cfcef9843295e51b10535ece5347 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "background.h"
#include "constants.h"
#include <raymath.h>
#include <assert.h>
#include <stdlib.h>
#include "utils.h"

void AddBackground(const char* backgroundTextureName) {
  Entity e = {0};

  e.type = Background_Entity;
  e.flags |= ENTITY_VISIBLE;

  e.position = (Vector2) {0.0f, 0.0f};

  Asset* backgroundTextureAsset =
    GetMatchingAssetWithType(backgroundTextureName, Texture_Asset);

  if (backgroundTextureAsset == NULL) {

    TraceLog(LOG_ERROR,
             "Failed to create background from texture asset: %s, asset not found",
             backgroundTextureName
             );
    exit(EXIT_FAILURE);

  }

  Texture2D backgroundTexture = backgroundTextureAsset->texture;
  const float backgroundSizeScale = (float)WINDOW_HEIGHT / backgroundTexture.height;

  Vector2 backgroundBounds =
    Vector2Scale((Vector2) {backgroundTexture.width, backgroundTexture.height}, backgroundSizeScale);

  Sprite backgroundSprite = (Sprite) {
    .texture = backgroundTexture,
    .layer = Background_Layer,
    .numAnimations = 0,

    .srcRect = {0, 0, backgroundTexture.width, backgroundTexture.height},
    .destRect = {NO_OFFSET, NO_OFFSET, backgroundBounds.x, backgroundBounds.y}
  };

  AddSpriteToEntity(&e, backgroundSprite);

  AddEntity(&e);
}