local changes before merge
This commit is contained in:
+157
@@ -1,6 +1,9 @@
|
||||
#include <iostream>
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "Vector2.h"
|
||||
#include "Vector3.h"
|
||||
#include "Vector4.h"
|
||||
@@ -15,6 +18,13 @@
|
||||
#include <cstdlib>
|
||||
#include "Vertex.h"
|
||||
#include "DepthBuffer.h"
|
||||
#include "SpriteAssetLoader.h"
|
||||
#include "SpriteRasterizer.h"
|
||||
#include "Image.h"
|
||||
#include "TomGameApp.h"
|
||||
#include "AudioInput.h"
|
||||
#include "AudioOutput.h"
|
||||
#include "ButtonInput.h"
|
||||
|
||||
#include "Display.h"
|
||||
#ifdef USE_FRAMEBUFFER
|
||||
@@ -25,6 +35,8 @@
|
||||
|
||||
const int32_t width = 800;
|
||||
const int32_t height = 600;
|
||||
const int32_t tomWidth = 800;
|
||||
const int32_t tomHeight = 480;
|
||||
|
||||
struct ProjectedVertex
|
||||
{
|
||||
@@ -103,8 +115,153 @@ static bool IsTriangleVisible(const CubeTriangle &triangle, const std::array<Mat
|
||||
return faceNormal.dot(faceCenter) > 0.0f;
|
||||
}
|
||||
|
||||
static bool HasArg(int argc, char* argv[], const std::string& expected)
|
||||
{
|
||||
for (int i = 1; i < argc; ++i)
|
||||
{
|
||||
if (argv[i] != nullptr && expected == argv[i])
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static std::string FindSpriteAssetPath(const std::string& fileName)
|
||||
{
|
||||
const char* roots[] = {
|
||||
"game/assets/sprites/",
|
||||
"../game/assets/sprites/",
|
||||
"../../game/assets/sprites/",
|
||||
"../../../game/assets/sprites/",
|
||||
"/tmp/game/assets/sprites/",
|
||||
"/usr/local/share/imx6u-game/sprites/"
|
||||
};
|
||||
|
||||
for (size_t i = 0; i < sizeof(roots) / sizeof(roots[0]); ++i)
|
||||
{
|
||||
const std::string path = std::string(roots[i]) + fileName;
|
||||
std::ifstream file(path.c_str(), std::ios::binary);
|
||||
if (file.good())
|
||||
{
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
return std::string("game/assets/sprites/") + fileName;
|
||||
}
|
||||
|
||||
static bool LoadSpriteAsset(const std::string& fileName, RenderData::Image& image)
|
||||
{
|
||||
const std::string path = FindSpriteAssetPath(fileName);
|
||||
if (!Asset::SpriteAssetLoader::Load(path, image))
|
||||
{
|
||||
std::cerr << "Load sprite asset failed: " << path << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static int RunTomGame()
|
||||
{
|
||||
#ifdef USE_FRAMEBUFFER
|
||||
Platform::IDisplay *display = new Platform::FBDisplay();
|
||||
#else
|
||||
Platform::IDisplay *display = new Platform::SDLDisplay();
|
||||
#endif
|
||||
|
||||
if (!display->init(tomWidth, tomHeight))
|
||||
{
|
||||
delete display;
|
||||
return -1;
|
||||
}
|
||||
|
||||
Core::FrameBuffer frameBuffer(tomWidth, tomHeight);
|
||||
Rasterizer::SpriteRasterizer spriteRasterizer(&frameBuffer);
|
||||
|
||||
RenderData::Image background;
|
||||
RenderData::Image tomStand;
|
||||
RenderData::Image tomListen;
|
||||
RenderData::Image tomSay1;
|
||||
RenderData::Image tomSay2;
|
||||
RenderData::Image tomSay3;
|
||||
RenderData::Image tomSay4;
|
||||
RenderData::Image button;
|
||||
|
||||
if (!LoadSpriteAsset("background.sprite", background) ||
|
||||
!LoadSpriteAsset("Tom-stand.sprite", tomStand) ||
|
||||
!LoadSpriteAsset("Tom-listhen.sprite", tomListen) ||
|
||||
!LoadSpriteAsset("Tom-say1.sprite", tomSay1) ||
|
||||
!LoadSpriteAsset("Tom-say2.sprite", tomSay2) ||
|
||||
!LoadSpriteAsset("Tom-say3.sprite", tomSay3) ||
|
||||
!LoadSpriteAsset("Tom-say4.sprite", tomSay4) ||
|
||||
!LoadSpriteAsset("ui-record.sprite", button))
|
||||
{
|
||||
std::cerr << "Run ConvertTomSprites before starting --tom." << std::endl;
|
||||
display->shutdown();
|
||||
delete display;
|
||||
return -1;
|
||||
}
|
||||
|
||||
Game::AudioInput audioInput;
|
||||
Game::AudioOutput audioOutput;
|
||||
Game::ButtonInput buttonInput;
|
||||
#if defined(__linux__)
|
||||
buttonInput.init();
|
||||
#endif
|
||||
|
||||
Game::TomGameAssets assets;
|
||||
assets.background = &background;
|
||||
assets.tomStand = &tomStand;
|
||||
assets.tomListen = &tomListen;
|
||||
assets.tomSay1 = &tomSay1;
|
||||
assets.tomSay2 = &tomSay2;
|
||||
assets.tomSay3 = &tomSay3;
|
||||
assets.tomSay4 = &tomSay4;
|
||||
assets.button = &button;
|
||||
|
||||
Game::TomGameApp app(&frameBuffer, &spriteRasterizer, &audioInput, &audioOutput, &buttonInput);
|
||||
app.set_assets(assets);
|
||||
if (!app.is_ready())
|
||||
{
|
||||
std::cerr << "TomGameApp assets are incomplete." << std::endl;
|
||||
display->shutdown();
|
||||
delete display;
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool isRunning = true;
|
||||
uint32_t lastTime = display->get_time_ms();
|
||||
while (isRunning)
|
||||
{
|
||||
display->poll_events(isRunning);
|
||||
const uint32_t currentTime = display->get_time_ms();
|
||||
const float deltaTime = static_cast<float>(currentTime - lastTime) * 0.001f;
|
||||
lastTime = currentTime;
|
||||
|
||||
app.update(deltaTime);
|
||||
frameBuffer.clear(RenderData::Color(18, 18, 24, 255));
|
||||
app.draw();
|
||||
display->present(&frameBuffer);
|
||||
}
|
||||
|
||||
buttonInput.shutdown();
|
||||
audioInput.shutdown();
|
||||
audioOutput.shutdown();
|
||||
display->shutdown();
|
||||
delete display;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (HasArg(argc, argv, "--tom"))
|
||||
{
|
||||
return RunTomGame();
|
||||
}
|
||||
|
||||
#ifdef USE_FRAMEBUFFER
|
||||
Platform::IDisplay *display = new Platform::FBDisplay();
|
||||
#else
|
||||
|
||||
Reference in New Issue
Block a user