/
raytmx-example.c
64 lines (54 loc) · 2.08 KB
/
raytmx-example.c
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <stddef.h> /* NULL */
#include <stdlib.h> /* EXIT_FAILURE, EXIT_SUCCESS */
#include "raylib.h"
#define RAYTMX_IMPLEMENTATION
#include "raytmx.h"
int main(int argc, char **argv) {
/* This map makes use of many TMX features making it useful for demonstrations. It will be adjacent to the */
/* executable once built. */
const char* tmx = "maps/raytmx-example.tmx";
/* Configure the window with a resolution and title. This example will also target 60 frames per second. */
const int screenWidth = 1400, screenHeight = 1400;
const float panSpeed = 150.0f;
InitWindow(screenWidth, screenHeight, "raytmx example");
SetTargetFPS(60);
/* Load the map. If loading fails, NULL will be returned and details will be TraceLog()'d. */
TmxMap* map = LoadTMX(tmx);
if (map == NULL) {
TraceLog(LOG_ERROR, "Failed to load TMX \"%s\"", tmx);
return EXIT_FAILURE;
}
/* Create a camera for efficient panning and zooming. The initial target will be the center of the map. */
Camera2D camera;
camera.zoom = 8.0f;
camera.target.x = (float)(map->width * map->tileWidth) / 2.0f;
camera.target.y = (float)(map->height * map->tileHeight) / 2.0f;
camera.offset.x = (float)screenWidth / 2.0f;
camera.offset.y = (float)screenHeight / 2.0f;
camera.rotation = 0.0f;
while (WindowShouldClose() == false) {
if (IsKeyDown(KEY_RIGHT))
camera.target.x += panSpeed * GetFrameTime();
if (IsKeyDown(KEY_LEFT))
camera.target.x -= panSpeed * GetFrameTime();
if (IsKeyDown(KEY_DOWN))
camera.target.y += panSpeed * GetFrameTime();
if (IsKeyDown(KEY_UP))
camera.target.y -= panSpeed * GetFrameTime();
BeginDrawing();
{
ClearBackground(BLACK);
BeginMode2D(camera);
{
AnimateTMX(map);
DrawTMX(map, &camera, 0, 0, WHITE);
}
EndMode2D();
DrawFPS(10, 10);
}
EndDrawing();
}
UnloadTMX(map);
CloseWindow();
return EXIT_SUCCESS;
}