summaryrefslogtreecommitdiff
path: root/code/game.c
blob: 8b37c99cf8a3275da2b7f2a00850722f01d603f1 (plain) (blame)
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

// TODO: figure out how to get those from javascript, because they are going to be different in WASM64
typedef int s32;
typedef unsigned int u32;
typedef unsigned char u8;
typedef float r32;
typedef int b32;
#define true 1
#define false 0

#define local_persist static
#define global static
#define internal static

#define WIDTH (1920/2)
#define HEIGHT (1080/2)
#define BYTES_PER_PIXEL 4

#define STB_SPRINTF_IMPLEMENTATION
#include "libs/stb_sprintf.h"

//- Global variables
extern u8 __heap_base;
u32 BumpPointer = (u32)&__heap_base;
u8 Buffer[WIDTH*HEIGHT*BYTES_PER_PIXEL];

//- Extern (js) functions
extern void LogMessage(u32 Length, char* message);

//- Memory
void* Malloc(u32 Size)
{
    u32 Result = BumpPointer;
    Result += Size;
    return (void *)Result;
}

void Free(u32 Size)
{
    BumpPointer -= Size;
}

//- Game
u32 GetBufferWidth() { return WIDTH; }
u32 GetBufferHeight() { return HEIGHT; }
u32 GetBytesPerPixel() { return BYTES_PER_PIXEL; }

void Logf(char *Format, ...)
{
    char MessageBuffer[256] = {0};
    
    va_list Args;
    va_start(Args, Format);
    u32 MessageLength = stbsp_vsprintf(MessageBuffer, Format, Args);
    
    LogMessage(MessageLength, MessageBuffer);
}

void RenderGradient(s32 Width, s32 Height, r32 dtForFrame)
{
    local_persist s32 Counter = 0;
    local_persist b32 Toggle = true;
    for(s32 Y = 0; Y < Height; Y++)
    {
        for(s32 X = 0; X < Width; X++)
        {
            r32 R = 0;
            r32 G = 0;
            r32 B = 0;
            
            if(Toggle)
            {
                R = 1.0f - ((r32)Counter/(r32)Width);
                G = ((r32)Counter/(r32)Width);
            }
            else
            {
                G = 1.0f - (r32)Counter/(r32)Width;
                R = (r32)Counter/(r32)Width;
            }
            
            // AA BB GG RR
            u32 Color = ((0xFF << 24) |
                         ((u32)(0xFF * B) << 16) |
                         ((u32)(0xFF * G) << 8)  |
                         ((u32)(0xFF * R) << 0)); 
            
            ((u32 *)Buffer)[Y*Width + X] = Color;
        }
    }
    
    Counter += (dtForFrame * 1000) * .3;
    if(Counter > Width)
    {
        Counter -= Width;
        Toggle = !Toggle;
    }
    
#if 1    
    Logf("%d", Width);
#endif
    
}