summaryrefslogtreecommitdiff
path: root/code/game.c
blob: 1159c1c656b95e01e9a0b8d6bd965de53d513381 (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
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
// NOTE(luca): Image buffer format is AA BB GG RR

//~ Types
// TODO: figure out how to get those from javascript, because they are going to be different in WASM64
typedef signed char    s8;
typedef signed short   s16;
typedef signed int     s32;
typedef unsigned char  u8;
typedef unsigned short u16;
typedef unsigned int   u32;
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

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

//~ Global variables
// From WASM
extern u8 __heap_base;

// Memory
u32 BumpPointer = (u32)&__heap_base;
u32 BumpAllocated = 0;

// Image
u8 Buffer[WIDTH*HEIGHT*BYTES_PER_PIXEL];

//~ Functions
//- Platform (js) 
extern void LogMessage(u32 Length, char* message);

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

void Free(u32 Size)
{
    BumpAllocated -= 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 
RenderRectangle(u8 *Buffer, s32 Pitch, s32 Width, s32 Height, s32 BytesPerPixel,
                s32 MinX, s32 MinY, s32 MaxX, s32 MaxY, u32 Color)
{
    if(MinX < 0)
    {
        MinX = 0;
    }
    
    if(MaxX > Width)
    {
        MaxX = Width;
    }
    
    if(MinY < 0)
    {
        MinY = 0;
    }
    
    if(MaxY > Height)
    {
        MaxY = Height;
    }
    
    u8 *Row = ((u8 *)Buffer) + ((BytesPerPixel*MinX) + (Pitch*MinY));
    
    for(s32 Y = MinY;
        Y < MaxY;
        Y++)
    {
        u32 *Pixel = (u32 *)Row;
        for(s32 X = MinX;
            X < MaxX;
            X++)
        {
            *Pixel++ = Color;
        }
        Row += Pitch;
    }
}

void 
UpdateAndRender(s32 Width, s32 Height, s32 BytesPerPixel, 
                r32 dtForFrame, b32 MouseDown, s32 MouseX, s32 MouseY)
{
#if 1
    // Clear the buffer to black.
    {    
        u32 *Clear = (u32 *)Buffer;
        s32 ClearSize = Width*Height;
        while(ClearSize--) *Clear++ = 0xFF000000;
    }
#endif
    
    u32 Color = 0;
    if(MouseDown)
    {
        Color = 0xFF00FF00;
    }
    
    u32 Pitch = BytesPerPixel * Width;
    
    s32 Size = 5;
    s32 MinX = MouseX - Size;
    s32 MaxX = MouseX + Size;
    s32 MinY = MouseY - Size;
    s32 MaxY = MouseY + Size;
    
    RenderRectangle(Buffer, Pitch, Width, Height, BytesPerPixel, 
                    MinX, MinY, MaxX, MaxY, Color);
    
#if 1    
    Logf("(%d, %d) / %s", 
         MouseX, MouseY, ((MouseDown) ? "down" : "up"));
#endif
    
}