aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: 5c7400bc853c32d6a07cd4129373ac2f6ab5ec10 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//#define LCB_NO_CONSOLE

#define SDL_MAIN_HANDLED
#include <SDL2/SDL.h> 
#include <SDL2/SDL_mixer.h> 

#include <stdio.h>
#include <math.h>

#include "json/jsonparse.h"
#include "render/render_text.h"
#include "render/graphicsettings.h"
#include "render/button.h"
#include "mkb/mkb.h"
#include "umath/vec.h"

#include "gst/units.h"
#include "gst/map.h"
#include "gst/info.h"
#include "gst/gst.h"
#include "gst/generate.h"

#include "hud/hud.h"
#include "net/net.h"

#define TEST 1
#if TEST == 1
#include "test/test.h"
#endif

//The music that will be played
Mix_Music *gMusic = NULL;

//The sound effects that will be used
Mix_Chunk *sounds[16];

int main( int argc, char* args[] ) {  
    graphic_settings gs = { 1250, 700 };
    
	SDL_Window* window = NULL;
	SDL_Surface* screenSurface = NULL;
    SDL_Renderer* rend = NULL;

	SDL_Init(SDL_INIT_VIDEO);
    
    net_init();
    generate_init();
    
    SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0");
    
    window = SDL_CreateWindow("DESCRIPT", SDL_WINDOWPOS_UNDEFINED, 
        SDL_WINDOWPOS_UNDEFINED, gs.resx, gs.resy, SDL_WINDOW_SHOWN 
        | SDL_WINDOW_RESIZABLE);
    screenSurface = SDL_GetWindowSurface(window);
    
    rend = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    
    // render text init
    txtd textd; char_width_init(textd.cw);
    SDL_Surface* image = SDL_LoadBMP("content/gf.bmp");
    textd.tex = SDL_CreateTextureFromSurface(rend, image);
    SDL_SetTextureColorMod(textd.tex, 0, 0, 0);
    
    SDL_Surface* imagesmall = SDL_LoadBMP("content/gfsmall.bmp");
    textd.tex_small = SDL_CreateTextureFromSurface(rend, imagesmall);
    SDL_SetTextureColorMod(textd.tex_small, 0, 0, 0);
    
    SDL_Surface* sprites = SDL_LoadBMP("content/sprites.bmp");
    SDL_Texture* txsprites = SDL_CreateTextureFromSurface(rend, sprites);
    
    Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 2048 );
    #define SOUND_MOUSE_OVER 0
    sounds[SOUND_MOUSE_OVER] = Mix_LoadWAV(
        "content/sounds/mouse_over.wav" );
    #define SOUND_MOUSE_CLICK_0 1
    sounds[SOUND_MOUSE_CLICK_0] = Mix_LoadWAV( 
        "content/sounds/mouse_click_0.wav" );
    #define SOUND_MOUSE_WHEEL 2
    sounds[SOUND_MOUSE_WHEEL] = Mix_LoadWAV(
        "content/sounds/mouse_wheel.wav" );
    #define SOUND_SUCCESS 3
    sounds[SOUND_SUCCESS] = Mix_LoadWAV( 
        "content/sounds/success.wav" );
    Mix_Volume(-1,MIX_MAX_VOLUME/16);
    
    // frame timing
    const double FRAME_TIME = (double)1/60; // delta time for 60 FPS
    double last_time = SDL_GetTicks();
    double frame_counter = 0;
    double tot_time = 0;
    double unprocessed_time = 0;
    int frames = 0;
    
    // mouse and keyboard
    MKb mkb;
    mkb_init(&mkb);
    
    // info
    infos info;
    info_load(&info);
    
    info_unit ugen; generate_unit(&info, &ugen, 10000);
    stats_unit base; stats_unit_compute(&info, &ugen, &base);
    stats_unit_printf(&info, &base);
    info_unit_printf(&ugen);
    printf("COST: %.2f\n", stats_compute_cost(&info.cost_weights, &base));
    
    gamestate gst;
    gst_init(&gst);
    gst.cam[0] = -gs.resx/2+gst.map_editor.sx*gst.map_editor.ts/2;
    gst.cam[1] = -gs.resy/2+gst.map_editor.sy*gst.map_editor.ts/2;

    
    // hud
    hud _hud;
    hud_init(&gs, &_hud, &textd);
    _hud.og.army_listlen = info_army_get_list(_hud.og.army_list);
    if (_hud.og.army_listlen > 0)
        info_load_army(gst.army_bp+0, _hud.og.army_list[0]);
    info_load_playername(_hud.og.playername);
    
    
    float mlast[2] = {0, 0};
    
    // fxs
    fxs fx;
    fx_init(&fx);
    
    // sockets
    net_client netc;
    net_server nets;
    int server = 0;
    
    int quit = 0;
    
    if (TEST) { 
        int result = test_run(&info); 
        if (result != 0) quit = 1;
    }
    
    SDL_Event e;
    while(!quit) {
        while(SDL_PollEvent(&e) != 0) {
            if(e.type == SDL_QUIT) {
                quit = 1;
            }
            if(e.type == SDL_WINDOWEVENT
            && e.window.event == SDL_WINDOWEVENT_RESIZED ) {
                gs.resx = e.window.data1; gs.resy = e.window.data2;
                SDL_SetWindowSize(window, gs.resx, gs.resy);
                hud_resize(&gs, &_hud, &textd);
            }
            mkb_event(&mkb, &e);
        }
        
        int render = 0;

        double startTime = SDL_GetTicks();
        double passedTime = (startTime - last_time)/1000;
        last_time = startTime;
        
        unprocessed_time += passedTime;
        frame_counter += passedTime;
        tot_time += passedTime;

        if (frame_counter >= 1.0) {
            //printf("FPS: %i | %f ms\n", frames, 1000.0 / ((double)frames));
            frames = 0;
            frame_counter = 0;
        }

        while (unprocessed_time > FRAME_TIME) {
            float mp[2] = { mkb.mx, mkb.my };
            if (mkb.mclick[1] == 1) {
                float delta[2] = { mkb.mx-mlast[0], mkb.my-mlast[1] };
                vec2_sub(gst.cam, gst.cam, delta);
            }
            
            hud_process(&gs, &_hud, &mkb, &info, 
                gst.army_bp+0, &gst.map_editor, &textd, &gst, 
                &netc, &nets, sounds);
                
            gst_process(&gst, &info, &fx, tot_time);
            fx_process(&fx, tot_time);
            
            render = 1;
            unprocessed_time -= FRAME_TIME;
            mlast[0] = mkb.mx; mlast[1] = mkb.my;
            mkb_process(&mkb);
        }

        if (render) {
            SDL_SetRenderDrawColor(rend, 255, 255, 255, 255);
            SDL_RenderClear(rend);
            
            gst_render(rend, txsprites, &textd, &gst, &info, tot_time);
            
            fx_render(rend, &fx, gst.cam, tot_time);
            
            hud_render(&_hud, rend, &textd, &mkb, &info, txsprites, &gst,
                tot_time);
            
            SDL_RenderPresent(rend);
            frames++;
        }
        else {
            SDL_Delay(1);
        }
    }

    Mix_FreeMusic(gMusic);
	SDL_DestroyWindow(window);
    Mix_Quit();
	SDL_Quit();
    net_client_close(&netc);
    net_server_close(&nets);
    gst_destroy(&gst);
    
	return 0;
}