summaryrefslogtreecommitdiff
path: root/main.cpp
blob: a958adb7ca4d5a5587e053aa783470615f6e2b2e (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <cstdlib>
#include <algorithm>
#include <raylib.h>

#include "lib/button.hpp"
#include "lib/dialogue.hpp"
#include "lib/gameState.hpp"
#include "screen/gameplay.cpp"
#include "screen/main_menu.cpp"
#include "lib/assetManager.hpp"
#include "lib/standard.hpp"

#if defined (__EMSCRIPTEN__)
#include <emscripten.h>
#include <emscripten/em_js.h>
#endif
// #include "lib/lbParser.hpp"

namespace {

void drawFrame(GameState& currentGameState) {
    UpdateMusicStream(currentGameState.BGMusic);
    // if (GetMusicTimePlayed(g_bgMusic) >= GetMusicTimeLength(g_bgMusic)) {
    //     SeekMusicStream(g_bgMusic, 0);
    // }
    RenderTexture screenTex = currentGameState.screenTex;
    float screenW = GetScreenWidth();
    float screenH = GetScreenHeight();
    float renderScale = currentGameState.renderScale;

    if (IsDialogRunning(currentGameState)) {
        currentGameState.DialogueOnlyInput = true;
        UpdateDialog(currentGameState);
    } else {
        currentGameState.DialogueOnlyInput = false;
    }

    BeginDrawing();
    ClearBackground(BLACK);
    if (true){ //screenW > screenH) {
        float scaleX = screenW / (float)VIRTUAL_SCREEN_W;
        float scaleY = screenH / (float)VIRTUAL_SCREEN_H;

        currentGameState.scale = std::min(scaleX, scaleY);

        float destW = VIRTUAL_SCREEN_W * currentGameState.scale;
        float destH = VIRTUAL_SCREEN_H * currentGameState.scale;

        float destX = (screenW - destW) * 0.5f;
        float destY = (screenH - destH) * 0.5f;

        Vector2 offset = { destX, destY };
        currentGameState.currentMousePos = ScreenToVirtual(GetMousePosition(), offset, currentGameState.scale);

        BeginTextureMode(screenTex);
        Camera2D camera = {};
        camera.target = {0.0f, 0.0f};
        camera.offset = {0.0f, 0.0f};
        camera.rotation = 0.0f;
        camera.zoom = renderScale;
        BeginMode2D(camera);
        switch (currentGameState.current_screen) {
        case MAIN_MENU:
            drawMainMenu(currentGameState);
            break;
        case GAMEPLAY:
            drawGameplay(currentGameState);
            break;
        }
        EndMode2D();
        EndTextureMode();

        DrawTexturePro(
            screenTex.texture,
            {0, 0, (float)screenTex.texture.width, -(float)screenTex.texture.height},
            {destX, destY, destW, destH},
            {0, 0},
            0,
            WHITE
        );}
    else {
        ui::DrawTextV("Please Play in\nLandscape\nMode", {10,50}, 40, WHITE);
    }
    // DrawText(TextFormat("%d, %f", 1, GetTime()), 0, 0, 20, YELLOW);
    EndDrawing();
}
}

void updateDrawFrame(void * arg){
    GameState* currentGameState = static_cast<GameState*>(arg);
    drawFrame(*currentGameState);
}
#if defined (__EMSCRIPTEN__)
EM_JS(int, isOnPhone, (), {
    const ua = navigator.userAgent || "";
    const mobileDetected =
        /Android|iPhone|iPad|iPod|Mobile|webOS|BlackBerry|IEMobile|Opera Mini/i.test(
            ua,
        ) ||
        (navigator.maxTouchPoints || 0) > 1 ||
        (window.matchMedia && window.matchMedia("(pointer: coarse)").matches);
    if (mobileDetected) {
        return 1;
    } else {
        return 0;
    }
})
#endif

int main() {
    SetConfigFlags(FLAG_WINDOW_RESIZABLE);
    InitWindow(VIRTUAL_SCREEN_W, VIRTUAL_SCREEN_H, "Cirno Day");
    // SetWindowOpacity(0.5f);
    GameState currentGameState;
    InitAudioDevice();
    srand(111111);
    ui::Init();
    currentGameState.numberFont = LoadFont("../assets/font/score.otf");

    // currentGameState.bulletList[0] = new Bullet{
    //     LoadTexture("assets/img/bullet/knife.png"),
    //     linear
    // };
    // TODO : Implement Asset Manager
    // DisableCursor();
    currentGameState.BGMusic = LoadMusicStream("../assets/audio/bgm/bgm.mp3");
    PlayMusicStream(currentGameState.BGMusic);
    currentGameState.AssetManager.init();
    #if defined (__EMSCRIPTEN__)
    currentGameState.touchScreenMode = (bool)isOnPhone();
    #else
    currentGameState.touchScreenMode = false;
    #endif
    currentGameState.renderScale = WEB_RENDER_SCALE;
    currentGameState.screenTex = LoadRenderTexture(
        (int)(VIRTUAL_SCREEN_W * currentGameState.renderScale),
        (int)(VIRTUAL_SCREEN_H * currentGameState.renderScale)
    );
    SetTextureFilter(currentGameState.screenTex.texture, TEXTURE_FILTER_BILINEAR);
    // int repeatInWidth = screenW/200;
    // int repeatinHeight = screenH/200;

    // BeginTextureMode(backgroundTex);
    //     for (int w = 0; w < repeatInWidth + 1; w++) {
    //         for (int h = 0; h < repeatinHeight + 1; h++) {
    //             DrawTextureV(currentGameState.AssetManager.load("hud_background"), {200.0f*w, 200.0f*h} , WHITE);
    //         }
    //     }
    // EndTextureMode();
    #if defined (__EMSCRIPTEN__)
        emscripten_set_main_loop_arg(updateDrawFrame, &currentGameState, 0, 1);
    #else
        SetTargetFPS(60);
        while(!WindowShouldClose()){
            drawFrame(currentGameState);
        }
    #endif

    currentGameState.AssetManager.cleanup();

    ui::Shutdown();
    CloseWindow();
    return 0;
}