package main

import "core:fmt"
import "core:time"

import "vendor:sdl2"

// Settings
WINDOW_WIDTH :: 640
WINDOW_HEIGHT :: 480
MAX_FPS :: 60


MINIMUM_DURATION_MICRO :: 1_000_000 / 60

main :: proc() {
	// Initialize SDL
	if sdl2.Init(sdl2.INIT_EVERYTHING) != 0 {
		fmt.eprintf("Error while initializing sdl: %s\n", sdl2.GetError())
		return
	}

	defer sdl2.Quit()

	window: ^sdl2.Window = ---
	renderer: ^sdl2.Renderer = ---

	// Initialize the renderer
	renderer_res := sdl2.CreateWindowAndRenderer(
		WINDOW_WIDTH,
		WINDOW_HEIGHT,
		sdl2.WINDOW_SHOWN,
		&window,
		&renderer,
	)

	if renderer_res != 0 {
		fmt.eprintf("Error while initializing renderer: %s\n", sdl2.GetError())
		return
	}

	defer sdl2.DestroyRenderer(renderer)
	defer sdl2.DestroyWindow(window)


	stopwatch: time.Stopwatch

	mainloop: for {
		// Reset the timer for FPS calculations
		time.stopwatch_reset(&stopwatch)
		time.stopwatch_start(&stopwatch)

		// Polling events
		event: sdl2.Event = ---
		for sdl2.PollEvent(&event) {
			#partial switch event.type {
			case .QUIT:
				break mainloop

			case .KEYDOWN:
				if event.key.keysym.sym == .ESCAPE {
					break mainloop
				}
			}

		}

		// Set color to White
		sdl2.SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF)

		// Clear the screen
		sdl2.RenderClear(renderer)

		// Set color to Red
		sdl2.SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0xFF)

		// Draw the 'H'
		sdl2.RenderDrawLine(renderer, 50, 50, 50, 150)
		sdl2.RenderDrawLine(renderer, 100, 50, 100, 150)
		sdl2.RenderDrawLine(renderer, 50, 100, 100, 100)

		// Draw the 'I'
		sdl2.RenderDrawLine(renderer, 150, 50, 150, 150)

		// Present
		sdl2.RenderPresent(renderer)


		// Fix the FPS
		time.stopwatch_stop(&stopwatch)
		elapsed := time.stopwatch_duration(stopwatch)
		time_to_wait := MINIMUM_DURATION_MICRO - time.duration_microseconds(elapsed)

		if time_to_wait > 0 {
			time.sleep(time.Duration(time_to_wait * 1000))
		}
	}
}

By admin

Leave a Reply

Your email address will not be published.