nitro-engine/examples/loading/simple_model/source/main.c
Antonio Niño Díaz 1acfca1a24 examples: Wait for VBL in the right place
This change is a bit pedantic, but it makes the ROMs run better in
DeSmuMe.

Some examples (particularly the dual 3D examples) used to flicker during
one or two seconds right after starting. In dual 3D examples the top and
bottom screen would start swapped, and they would eventually swap and
stop flickering. This would never happen in melonDS or real hardware.

I suspect this is because of the interaction between GFX_FLUSH and
swiWaitForVBlank(), where there would be some timing difference to reach
the first swiWaitForVBlank() or GFX_FLUSH, and that caused the desync.

This commit moves swiWaitForVBlank() to the beginning of the game loop.
This means that, even in the first iteration of the game loop, all
emulators and hardware will be synchronized. This doesn't actually
matter in any other situation, it just makes the first iteration
consistent.
2023-01-21 19:32:02 +00:00

96 lines
2.5 KiB
C

// SPDX-License-Identifier: CC0-1.0
//
// SPDX-FileContributor: Antonio Niño Díaz, 2008-2011, 2019, 2022
//
// This file is part of Nitro Engine
#include <NEMain.h>
// First you have to put the .bin files in the data folder. This will generate
// (after doing "make") some files named "binfilename_bin.h". For example,
// "model.bin" will generate a file named "model_bin.h". You have to include
// this in "main.c".
//
// The name you will have to use is "binfilename_bin". For example, for loading
// "model.bin" you will have to use:
//
// NE_ModelLoadStaticMesh(Model, (u32 *)binfilename_bin);
//
#include "robot_bin.h"
#include "texture_tex_bin.h"
NE_Camera *Camera;
NE_Model *Model;
NE_Material *Material;
void Draw3DScene(void)
{
NE_CameraUse(Camera);
NE_ModelDraw(Model);
}
int main(void)
{
irqEnable(IRQ_HBLANK);
irqSet(IRQ_VBLANK, NE_VBLFunc);
irqSet(IRQ_HBLANK, NE_HBLFunc);
// Init Nitro Engine in normal 3D mode
NE_Init3D();
// libnds uses VRAM_C for the text console, reserve A and B only
NE_TextureSystemReset(0, 0, NE_VRAM_AB);
// Init console in non-3D screen
consoleDemoInit();
// Allocate space for the objects we'll use
Model = NE_ModelCreate(NE_Static);
Camera = NE_CameraCreate();
Material = NE_MaterialCreate();
// Set coordinates for the camera
NE_CameraSet(Camera,
-8, 3, 0, // Position
0, 3, 0, // Look at
0, 1, 0); // Up direction
// Load mesh from RAM and assign it to the object "Model".
NE_ModelLoadStaticMesh(Model, (u32 *)robot_bin);
// Load a RGB texture from RAM and assign it to "Material".
NE_MaterialTexLoad(Material, NE_RGB5, 256, 256, NE_TEXGEN_TEXCOORD,
(u8 *)texture_tex_bin);
// Assign texture to model...
NE_ModelSetMaterial(Model, Material);
// We set up a light and its color
NE_LightSet(0, NE_White, -0.5, -0.5, -0.5);
while (1)
{
// Wait for next frame
NE_WaitForVBL(0);
// Get keys information
scanKeys();
uint32 keys = keysHeld();
printf("\x1b[0;0HPad: Rotate.");
// Rotate model using the pad
if (keys & KEY_UP)
NE_ModelRotate(Model, 0, 0, -2);
if (keys & KEY_DOWN)
NE_ModelRotate(Model, 0, 0, 2);
if (keys & KEY_RIGHT)
NE_ModelRotate(Model, 0, 2, 0);
if (keys & KEY_LEFT)
NE_ModelRotate(Model, 0, -2, 0);
// Draw scene
NE_Process(Draw3DScene);
}
return 0;
}