~zjm/Moon3D

ref: 0e81b821a0782674dbed435a58ad682e7aef0282 Moon3D/src/sketch.c -rw-r--r-- 1.6 KiB
0e81b821Zack Michener wrap rendering objects in a RenderingContext struct 4 months ago
                                                                                
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
#include "sketch.h"
#include "arraylist.h"
#include "scenes/cubeworld.h"
#include "player.h"

int debounce = 0;
bool dither = false;
Scene *scene;
static SpotLightSource *flashlight;
int now, lastTime;
RenderingContext context;

void sketch_setup(Pixel *pixels)
{
	Viewport *viewport = NewViewport(pixels, BLACK, 400, 240);
	RasterSettings settings = DefaultRasterSettings();
	scene = CubeWorld();
	context = MakeRenderingContext(viewport, &scene->player->camera, settings);

	flashlight = SpotLight(CameraPosition(), CameraDirection(), 0.1, 0);
	AddLight(scene, (LightSource *)flashlight);
}

void sketch_draw(long ticks)
{
	int dt = ticks - lastTime;
	lastTime = ticks;
	ClearViewport(context.viewport);

	(*(scene->onUpdate))(dt);
	RenderScene(scene, context);

	if (dither) {
		Dither(context.viewport);
	}
}

void sketch_input(InputState input)
{
	if (KeyPressed(input, KEY_LEFT)) {
		PlayerTurnLeft(scene->player, 5);
		flashlight->direction = PlayerDirection(scene->player);
	}
	if (KeyPressed(input, KEY_RIGHT)) {
		PlayerTurnRight(scene->player, 5);
		flashlight->direction = PlayerDirection(scene->player);
	}
	if (KeyPressed(input, KEY_FWD)) {
		PlayerMoveForward(scene->player, 0.5);
		((LightSource *)flashlight)->position = PlayerPosition(scene->player);
	}
	if (KeyPressed(input, KEY_BACK)) {
		PlayerMoveBackward(scene->player, 0.5);
		((LightSource *)flashlight)->position = PlayerPosition(scene->player);
	}
	if (debounce == 0 && KeyPressed(input, KEY_JUMP)) {
		debounce = 10;
		// dither = !dither;
		((LightSource *)flashlight)->intensity = (((LightSource *)flashlight)->intensity) ? 0 : 0.5;
	}

	if (debounce > 0) debounce--;
}