aboutsummaryrefslogtreecommitdiff
path: root/src/main.odin
blob: e1b13d55e9228bfe3cd03a51204f737bc74ba037 (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
package demonchime

import "base:runtime"

import "core:fmt"
import "core:log"
import "core:mem"
import hm "core:container/handle_map"

import "phys"
import "fw"

Handle :: hm.Handle32

Vec2 :: fw.Vec2
Vec2i :: fw.Vec2i
Color :: fw.Color
Rect :: fw.Rect

ROOM_FADE_SIZE :: 8

FIXED_UPDATE_RATE :: 1.0/60

SCREEN_WIDTH :: 256
SCREEN_HEIGHT :: 256
SCREEN_SIZE :: Vec2{SCREEN_WIDTH, SCREEN_HEIGHT}
SCREEN_SIZE_INT :: Vec2i{SCREEN_WIDTH, SCREEN_HEIGHT}

state: struct {
  debug_mode: bool,
  camera_target: Vec2,
  camera_pos: Vec2,
  last_fixed_update: f64,
  prop_list: hm.Dynamic_Handle_Map(Prop, Handle),
  wiggle_prop_list: hm.Dynamic_Handle_Map(Wiggle_Prop, Handle),
  platform_list: hm.Dynamic_Handle_Map(Platform, Handle),
  bullet_list: hm.Dynamic_Handle_Map(Bullet, Handle),
  rope_list: hm.Dynamic_Handle_Map(Rope, Handle),
}

logger: log.Logger

init :: proc() {
  fw.init({
    win_name = "Demonchime",
    win_size = SCREEN_SIZE_INT * 3, 
    screen_size = SCREEN_SIZE_INT,
    renderer_backend = .OpenGL,
  })

  init_keybinds()
  init_player()
  open_room(.Carrabassett0)
}

frame :: proc() {
  update_keybinds()

  if fw.is_keybind_just_down(actions.toggle_debug_mode) {
    state.debug_mode = !state.debug_mode
  }

  if fw.is_keybind_just_down(actions.toggle_fullscreen) {
    // rl.ToggleBorderlessWindowed()
    // rl.ToggleFullscreen()
  }

  dt := f32(fw.get_delta_time())

  if fw.get_time() - state.last_fixed_update > FIXED_UPDATE_RATE {
    state.last_fixed_update = fw.get_time()
    for cb in fixed_update_callbacks {
      cb(FIXED_UPDATE_RATE)
    }
  }

  for cb in update_callbacks {
    cb(dt)
  }

  state.camera_pos = dt_lerp(
    state.camera_pos,
    state.camera_target,
    CAMERA_ACCEL,
  )

  fw.set_camera_pos(state.camera_pos)

  fw.draw_rect(
    {0, 0},
    {f32(current_room.width), f32(current_room.height)},
    color = get_room(current_room.id).background_color,
  )


  for cb in draw_callbacks {
    cb()
  }
  draw_room(current_room.id)
  for cb in fg_draw_callbacks {
    cb()
  }

  fw.draw_rect_gradient(
    {0, 0},
    {ROOM_FADE_SIZE, f32(current_room.height)},
    tl_color = fw.BLACK, tr_color = fw.TRANSPARENT,
    bl_color = fw.BLACK, br_color = fw.TRANSPARENT,
  )
  fw.draw_rect_gradient(
    {f32(current_room.width) - ROOM_FADE_SIZE, 0},
    {ROOM_FADE_SIZE, f32(current_room.height)},
    tl_color = fw.TRANSPARENT, tr_color = fw.BLACK,
    bl_color = fw.TRANSPARENT, br_color = fw.BLACK,
  )

  fw.draw_rect_gradient(
    {0, 0},
    {f32(current_room.width), ROOM_FADE_SIZE},
    tl_color = fw.BLACK, tr_color = fw.BLACK,
    bl_color = fw.TRANSPARENT, br_color = fw.TRANSPARENT,
  )
  fw.draw_rect_gradient(
    {0, f32(current_room.height) - ROOM_FADE_SIZE},
    {f32(current_room.width), ROOM_FADE_SIZE},
    tl_color = fw.TRANSPARENT, tr_color = fw.TRANSPARENT,
    bl_color = fw.BLACK, br_color = fw.BLACK,
  )

  if state.debug_mode {
    // Draw all collisions
    body_it: int
    color := Color{1, 0.25, 0.5, 0.25}
    body_iter := hm.iterator_make(&phys.world.bodies)
    for _, body in hm.iterate(&body_iter) {
      rect := phys.get_rect(body)
      rect.start += phys.get_position(body)
      fw.draw_rect(rect.start, rect.size, color = color, lines = true)
      fw.draw_rect(rect.start, rect.size, color = color)
    }
  }

  // fps_text := fmt.caprintf(
  //   "FPS: %v",
  //   rl.GetFPS(),
  //   allocator = context.temp_allocator,
  // )
  // rl.DrawText(fps_text, 5, 5, 8, rl.GREEN)
  //
  // if state.debug_mode {
  //   rl.DrawText("Debug Mode", 5, 5 + 8, 8, rl.YELLOW)
  // }
}

cleanup :: proc() {
  deinit_player()
  hm.dynamic_destroy(&state.platform_list)
  hm.dynamic_destroy(&state.bullet_list)
  hm.dynamic_destroy(&state.prop_list)
  hm.dynamic_destroy(&state.wiggle_prop_list)

  clear_ropes()
  hm.dynamic_destroy(&state.rope_list)

  phys.destroy_world()

  fw.deinit()
}

main :: proc() {
  when ODIN_DEBUG {
    track: mem.Tracking_Allocator
    mem.tracking_allocator_init(&track, context.allocator)
    context.allocator = mem.tracking_allocator(&track)

    defer {
      if len(track.allocation_map) > 0 {
        fmt.eprintf(
          "=== %v allocations not freed: ===\n",
          len(track.allocation_map),
        )
        total := 0
        for _, entry in track.allocation_map {
          fmt.eprintf(
            "- %v bytes @ %v (%v, mode %v)\n",
            entry.size,
            entry.location,
            entry.err,
            entry.mode,
          )
          total += entry.size
        }

        fmt.eprintf("=== a total of %v bytes unfreed ===\n", total)
      }
      mem.tracking_allocator_destroy(&track)
    }
  }

  logger = log.create_console_logger()
  context.logger = logger
  defer log.destroy_console_logger(logger)

  init()

  for fw.next_frame() {
    frame()

    if fw.is_keybind_just_down(actions.exit) {
      break
    }
  }

  cleanup()
}