boo/test/main.cpp

356 lines
10 KiB
C++
Raw Normal View History

2015-04-18 21:46:51 +00:00
#include <stdio.h>
#include <math.h>
2015-08-18 22:43:30 +00:00
#include <boo/boo.hpp>
#include <boo/graphicsdev/GL.hpp>
2015-10-30 06:26:02 +00:00
#include <thread>
#include <mutex>
#include <condition_variable>
#include <LogVisor/LogVisor.hpp>
2015-04-18 21:46:51 +00:00
2015-04-29 10:24:39 +00:00
namespace boo
{
2015-08-18 19:40:26 +00:00
class DolphinSmashAdapterCallback : public IDolphinSmashAdapterCallback
2015-04-24 00:24:15 +00:00
{
void controllerConnected(unsigned idx, EDolphinControllerType)
2015-04-24 00:24:15 +00:00
{
printf("CONTROLLER %u CONNECTED\n", idx);
}
void controllerDisconnected(unsigned idx, EDolphinControllerType)
2015-04-24 00:24:15 +00:00
{
printf("CONTROLLER %u DISCONNECTED\n", idx);
}
void controllerUpdate(unsigned idx, EDolphinControllerType,
2015-08-18 19:40:26 +00:00
const DolphinControllerState& state)
2015-04-24 00:24:15 +00:00
{
printf("CONTROLLER %u UPDATE %d %d\n", idx, state.m_leftStick[0], state.m_leftStick[1]);
2015-05-15 01:16:36 +00:00
printf(" %d %d\n", state.m_rightStick[0], state.m_rightStick[1]);
}
};
2015-08-18 23:38:53 +00:00
class DualshockPadCallback : public IDualshockPadCallback
2015-05-15 01:16:36 +00:00
{
void controllerDisconnected()
{
printf("CONTROLLER DISCONNECTED\n");
}
2015-08-18 23:38:53 +00:00
void controllerUpdate(const DualshockPadState& state)
2015-05-15 01:16:36 +00:00
{
static time_t timeTotal;
static time_t lastTime = 0;
timeTotal = time(NULL);
time_t timeDif = timeTotal - lastTime;
/*
if (timeDif >= .15)
{
uint8_t led = ctrl->getLED();
led *= 2;
if (led > 0x10)
led = 2;
ctrl->setRawLED(led);
lastTime = timeTotal;
}
*/
if (state.m_psButtonState)
{
if (timeDif >= 1) // wait 30 seconds before issuing another rumble event
{
ctrl->startRumble(DS3_MOTOR_LEFT);
ctrl->startRumble(DS3_MOTOR_RIGHT, 100);
lastTime = timeTotal;
}
}
/*
else
ctrl->stopRumble(DS3_MOTOR_RIGHT | DS3_MOTOR_LEFT);*/
2015-05-22 00:35:46 +00:00
2015-05-15 01:16:36 +00:00
printf("CONTROLLER UPDATE %d %d\n", state.m_leftStick[0], state.m_leftStick[1]);
printf(" %d %d\n", state.m_rightStick[0], state.m_rightStick[1]);
printf(" %f %f %f\n", state.accPitch, state.accYaw, state.gyroZ);
2015-04-24 00:24:15 +00:00
}
};
2015-08-18 19:40:26 +00:00
class TestDeviceFinder : public DeviceFinder
2015-04-22 21:48:23 +00:00
{
2015-08-18 19:40:26 +00:00
DolphinSmashAdapter* smashAdapter = NULL;
DualshockPad* ds3 = nullptr;
2015-08-18 19:40:26 +00:00
DolphinSmashAdapterCallback m_cb;
DualshockPadCallback m_ds3CB;
2015-04-22 21:48:23 +00:00
public:
2015-08-18 19:40:26 +00:00
TestDeviceFinder()
: DeviceFinder({typeid(DolphinSmashAdapter)})
2015-04-22 21:48:23 +00:00
{}
2015-08-18 19:40:26 +00:00
void deviceConnected(DeviceToken& tok)
2015-04-22 21:48:23 +00:00
{
2015-08-18 19:40:26 +00:00
smashAdapter = dynamic_cast<DolphinSmashAdapter*>(tok.openAndGetDevice());
2015-05-15 01:16:36 +00:00
if (smashAdapter)
{
smashAdapter->setCallback(&m_cb);
smashAdapter->startRumble(0);
return;
}
ds3 = dynamic_cast<DualshockPad*>(tok.openAndGetDevice());
2015-05-15 01:16:36 +00:00
if (ds3)
{
ds3->setCallback(&m_ds3CB);
ds3->setLED(DS3_LED_1);
}
2015-04-22 21:48:23 +00:00
}
2015-08-18 19:40:26 +00:00
void deviceDisconnected(DeviceToken&, DeviceBase* device)
2015-04-22 21:48:23 +00:00
{
if (smashAdapter == device)
{
delete smashAdapter;
smashAdapter = NULL;
}
2015-05-15 01:16:36 +00:00
if (ds3 == device)
{
delete ds3;
ds3 = nullptr;
}
2015-04-22 21:48:23 +00:00
}
};
2015-05-04 04:28:07 +00:00
2015-08-18 19:40:26 +00:00
struct CTestWindowCallback : IWindowCallback
2015-05-04 04:28:07 +00:00
{
2015-11-02 09:31:06 +00:00
void resized(const SWindowRect& rect)
{ fprintf(stderr, "Resized %d, %d (%d, %d)\n", rect.size[0], rect.size[1], rect.location[0], rect.location[1]); }
2015-05-12 09:38:37 +00:00
void mouseDown(const SWindowCoord& coord, EMouseButton button, EModifierKey mods)
2015-05-04 04:28:07 +00:00
{
2015-05-12 09:38:37 +00:00
fprintf(stderr, "Mouse Down %d (%f,%f)\n", button, coord.norm[0], coord.norm[1]);
}
void mouseUp(const SWindowCoord& coord, EMouseButton button, EModifierKey mods)
{
fprintf(stderr, "Mouse Up %d (%f,%f)\n", button, coord.norm[0], coord.norm[1]);
}
void mouseMove(const SWindowCoord& coord)
{
2015-11-02 09:31:06 +00:00
fprintf(stderr, "Mouse Move (%f,%f)\n", coord.norm[0], coord.norm[1]);
2015-05-12 09:38:37 +00:00
}
void scroll(const SWindowCoord& coord, const SScrollDelta& scroll)
{
2015-05-13 08:51:18 +00:00
fprintf(stderr, "Mouse Scroll (%f,%f) (%f,%f)\n", coord.norm[0], coord.norm[1], scroll.delta[0], scroll.delta[1]);
2015-05-04 04:28:07 +00:00
}
2015-05-13 08:51:18 +00:00
void touchDown(const STouchCoord& coord, uintptr_t tid)
2015-05-04 04:28:07 +00:00
{
2015-05-13 08:51:18 +00:00
//fprintf(stderr, "Touch Down %16lX (%f,%f)\n", tid, coord.coord[0], coord.coord[1]);
2015-05-12 09:38:37 +00:00
}
2015-05-13 08:51:18 +00:00
void touchUp(const STouchCoord& coord, uintptr_t tid)
2015-05-12 09:38:37 +00:00
{
2015-05-13 08:51:18 +00:00
//fprintf(stderr, "Touch Up %16lX (%f,%f)\n", tid, coord.coord[0], coord.coord[1]);
2015-05-12 09:38:37 +00:00
}
2015-05-13 08:51:18 +00:00
void touchMove(const STouchCoord& coord, uintptr_t tid)
2015-05-12 09:38:37 +00:00
{
2015-05-13 08:51:18 +00:00
//fprintf(stderr, "Touch Move %16lX (%f,%f)\n", tid, coord.coord[0], coord.coord[1]);
2015-05-12 09:38:37 +00:00
}
2015-05-04 04:28:07 +00:00
2015-05-12 09:38:37 +00:00
void charKeyDown(unsigned long charCode, EModifierKey mods, bool isRepeat)
{
2015-05-04 04:28:07 +00:00
2015-05-12 09:38:37 +00:00
}
void charKeyUp(unsigned long charCode, EModifierKey mods)
{
2015-05-04 04:28:07 +00:00
2015-05-12 09:38:37 +00:00
}
void specialKeyDown(ESpecialKey key, EModifierKey mods, bool isRepeat)
{
2015-05-04 04:28:07 +00:00
2015-05-12 09:38:37 +00:00
}
void specialKeyUp(ESpecialKey key, EModifierKey mods)
2015-05-04 04:28:07 +00:00
{
2015-05-12 09:38:37 +00:00
2015-05-04 04:28:07 +00:00
}
2015-05-12 09:38:37 +00:00
void modKeyDown(EModifierKey mod, bool isRepeat)
{
2015-05-04 04:28:07 +00:00
2015-05-12 09:38:37 +00:00
}
void modKeyUp(EModifierKey mod)
{
2015-05-04 04:28:07 +00:00
2015-05-12 09:38:37 +00:00
}
2015-05-04 04:28:07 +00:00
2015-05-12 09:38:37 +00:00
};
2015-08-18 19:40:26 +00:00
struct TestApplicationCallback : IApplicationCallback
2015-05-04 04:28:07 +00:00
{
2015-11-01 00:06:56 +00:00
IWindow* mainWindow;
2015-08-18 19:40:26 +00:00
boo::TestDeviceFinder devFinder;
2015-05-12 09:38:37 +00:00
CTestWindowCallback windowCallback;
bool running = true;
2015-10-30 06:26:02 +00:00
const IShaderDataBinding* m_binding = nullptr;
std::mutex m_mt;
std::condition_variable m_cv;
static void LoaderProc(TestApplicationCallback* self)
{
GLES3DataFactory* factory =
dynamic_cast<GLES3DataFactory*>(self->mainWindow->getLoadContextDataFactory());
/* Make Tri-strip VBO */
struct Vert
{
float pos[3];
float uv[2];
};
static const Vert quad[4] =
{
2015-11-01 00:06:56 +00:00
{{0.5,0.5},{1.0,1.0}},
{{-0.5,0.5},{0.0,1.0}},
{{0.5,-0.5},{1.0,0.0}},
{{-0.5,-0.5},{0.0,0.0}}
2015-10-30 06:26:02 +00:00
};
2015-11-02 09:31:06 +00:00
IGraphicsBuffer* vbo =
factory->newStaticBuffer(BufferUseVertex, quad, sizeof(quad), 4);
2015-10-30 06:26:02 +00:00
/* Make vertex format */
2015-11-02 09:31:06 +00:00
VertexElementDescriptor descs[2] =
2015-10-30 06:26:02 +00:00
{
{vbo, nullptr, VertexSemanticPosition},
{vbo, nullptr, VertexSemanticUV}
};
2015-11-02 09:31:06 +00:00
IVertexFormat* vfmt = factory->newVertexFormat(2, descs);
2015-10-30 06:26:02 +00:00
/* Make ramp texture */
using Pixel = uint8_t[4];
static Pixel tex[256][256];
for (int i=0 ; i<256 ; ++i)
for (int j=0 ; j<256 ; ++j)
{
tex[i][j][0] = i;
tex[i][j][1] = j;
tex[i][j][2] = 0;
tex[i][j][3] = 0xff;
}
2015-11-02 09:31:06 +00:00
ITexture* texture =
2015-10-30 06:26:02 +00:00
factory->newStaticTexture(256, 256, 1, TextureFormatRGBA8, tex, 256*256*4);
/* Make shader pipeline */
static const char* VS =
2015-10-31 19:21:23 +00:00
"#version 330\n"
2015-10-30 06:26:02 +00:00
"layout(location=0) in vec3 in_pos;\n"
"layout(location=1) in vec2 in_uv;\n"
"out vec2 out_uv;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(in_pos, 1.0);\n"
2015-10-30 06:26:02 +00:00
" out_uv = in_uv;\n"
"}\n";
static const char* FS =
2015-10-31 19:21:23 +00:00
"#version 330\n"
"precision highp float;\n"
2015-11-01 00:06:56 +00:00
"uniform sampler2D smplr;\n"
2015-10-30 06:26:02 +00:00
"layout(location=0) out vec4 out_frag;\n"
"in vec2 out_uv;\n"
"void main()\n"
"{\n"
2015-11-01 00:06:56 +00:00
" out_frag = texture(smplr, out_uv);\n"
2015-10-30 06:26:02 +00:00
"}\n";
2015-11-01 00:06:56 +00:00
static const char* TexNames[] = {"smplr"};
2015-11-02 09:31:06 +00:00
IShaderPipeline* pipeline =
factory->newShaderPipeline(VS, FS, 1, TexNames, BlendFactorOne, BlendFactorZero, true, true, false);
2015-10-30 06:26:02 +00:00
/* Make shader data binding */
self->m_binding =
factory->newShaderDataBinding(pipeline, vfmt, vbo, nullptr, 0, nullptr, 1, &texture);
/* Commit objects */
2015-11-01 00:06:56 +00:00
IGraphicsData* data = factory->commit();
2015-10-30 06:26:02 +00:00
/* Wait for exit */
while (self->running)
{
{
std::unique_lock<std::mutex> lk(self->m_mt);
self->m_cv.wait(lk);
if (!self->running)
break;
}
}
}
int appMain(IApplication* app)
2015-04-18 23:12:22 +00:00
{
2015-08-31 03:40:58 +00:00
mainWindow = app->newWindow(_S("YAY!"));
2015-05-12 09:38:37 +00:00
mainWindow->setCallback(&windowCallback);
2015-05-06 00:50:57 +00:00
mainWindow->showWindow();
2015-10-31 19:21:23 +00:00
//mainWindow->setFullscreen(true);
2015-05-06 00:50:57 +00:00
devFinder.startScanning();
2015-10-30 06:26:02 +00:00
IGraphicsCommandQueue* gfxQ = mainWindow->getCommandQueue();
std::thread loaderThread(LoaderProc, this);
size_t frameIdx = 0;
2015-10-31 06:39:11 +00:00
size_t lastCheck = 0;
while (running)
{
mainWindow->waitForRetrace();
float rgba[] = {sinf(frameIdx / 60.0), cosf(frameIdx / 60.0), 0.0, 1.0};
gfxQ->setClearColor(rgba);
gfxQ->clearTarget();
2015-10-30 06:26:02 +00:00
if (m_binding)
{
gfxQ->setDrawPrimitive(PrimitiveTriStrips);
gfxQ->setShaderDataBinding(m_binding);
gfxQ->draw(0, 4);
}
gfxQ->present();
gfxQ->execute();
2015-11-02 09:31:06 +00:00
//fprintf(stderr, "%zu\n", frameIdx);
2015-10-31 19:21:23 +00:00
++frameIdx;
2015-10-31 06:39:11 +00:00
if ((frameIdx - lastCheck) > 100)
{
lastCheck = frameIdx;
2015-10-31 19:21:23 +00:00
//mainWindow->setFullscreen(!mainWindow->isFullscreen());
2015-10-31 06:39:11 +00:00
}
}
2015-10-30 06:26:02 +00:00
m_cv.notify_one();
loaderThread.join();
return 0;
2015-04-18 23:12:22 +00:00
}
2015-05-06 00:50:57 +00:00
void appQuitting(IApplication*)
2015-05-04 04:28:07 +00:00
{
running = false;
2015-05-06 00:50:57 +00:00
}
2015-08-31 03:40:58 +00:00
void appFilesOpen(IApplication*, const std::vector<SystemString>& paths)
2015-05-15 01:16:36 +00:00
{
2015-05-13 22:21:13 +00:00
fprintf(stderr, "OPENING: ");
2015-08-31 03:40:58 +00:00
for (const SystemString& path : paths)
{
#if _WIN32
fwprintf(stderr, L"%s ", path.c_str());
#else
2015-05-13 22:21:13 +00:00
fprintf(stderr, "%s ", path.c_str());
2015-08-31 03:40:58 +00:00
#endif
}
2015-05-13 22:21:13 +00:00
fprintf(stderr, "\n");
2015-05-15 01:16:36 +00:00
}
2015-05-06 00:50:57 +00:00
};
2015-05-04 04:28:07 +00:00
}
2015-08-31 03:40:58 +00:00
#ifdef _WIN32
int wmain(int argc, const wchar_t** argv)
#else
2015-08-18 22:43:30 +00:00
int main(int argc, const char** argv)
2015-08-31 03:40:58 +00:00
#endif
2015-04-18 21:46:51 +00:00
{
LogVisor::RegisterConsoleLogger();
2015-08-18 19:40:26 +00:00
boo::TestApplicationCallback appCb;
2015-11-01 00:06:56 +00:00
int ret = ApplicationRun(boo::IApplication::PLAT_AUTO,
appCb, _S("rwk"), _S("RWK"), argc, argv);
2015-05-12 09:38:37 +00:00
printf("IM DYING!!\n");
2015-10-28 01:47:55 +00:00
return ret;
2015-04-18 21:46:51 +00:00
}
2015-05-04 04:28:07 +00:00