begin working on simple gfx wrapper
authorslack <slack@codemaniacs.com>
Tue, 30 Sep 2008 20:03:42 +0000 (22:03 +0200)
committerslack <slack@codemaniacs.com>
Tue, 30 Sep 2008 20:03:42 +0000 (22:03 +0200)
gfx.cc [new file with mode: 0644]
gfx.h [new file with mode: 0644]
logger.h [new file with mode: 0644]
singleton.h [new file with mode: 0644]

diff --git a/gfx.cc b/gfx.cc
new file mode 100644 (file)
index 0000000..e5905cb
--- /dev/null
+++ b/gfx.cc
@@ -0,0 +1,69 @@
+#include "gfx.h"
+
+#include <pspgu.h>
+#include <pspgum.h>
+#include <psprtc.h>    
+
+void GFX::init()
+{
+       dList = memalign(16, 640);
+       fbp0  = 0;
+
+       // Init GU
+       sceGuInit();
+       sceGuStart(GU_DIRECT, display_list);
+       
+       // Set Buffers
+       sceGuDrawBuffer(GU_PSM_8888, fbp0, BUF_WIDTH);
+       sceGuDispBuffer(SCR_WIDTH, SCR_HEIGHT, (void*)0x88000, BUF_WIDTH);
+       sceGuDepthBuffer((void*)0x110000, BUF_WIDTH);
+       sceGuOffset(2048 - (SCR_WIDTH/2), 2048 - (SCR_HEIGHT/2));
+       sceGuViewport(2048, 2048, SCR_WIDTH, SCR_HEIGHT);
+       sceGuDepthRange(65535, 0);
+       
+       // Set Render States
+       // Enable scissor test, depth test, smooth shading and culling
+       sceGuScissor( 0, 0, SCR_WIDTH, SCR_HEIGHT);
+       sceGuEnable( GU_SCISSOR_TEST );
+       sceGuDepthFunc( GU_GEQUAL );
+       sceGuEnable( GU_DEPTH_TEST );
+       sceGuShadeModel( GU_SMOOTH );
+       sceGuFrontFace( GU_CW );
+       sceGuEnable( GU_CULL_FACE );
+       sceGuEnable( GU_CLIP_PLANES );
+    sceGuFinish();
+       sceGuSync(0,0);
+       sceDisplayWaitVblankStart();
+       sceGuDisplay(GU_TRUE);
+       // finish
+
+       // init fps counter
+       sceRtcGetCurrentTick( &fpsTickLast );
+       tickResolution = sceRtcGetTickResolution();
+}
+
+}
+
+void GFX::cleanup()
+{
+       sceGuTerm();
+}
+
+void update_fps( void )
+{
+       frames_drawn++;
+       sceRtcGetCurrentTick( &fpsTickNow );
+
+       if( ((fps_tick_now - fps_tick_last)/((float)tick_resolution)) >= 1.0f )
+       {
+               fps_tick_last = fps_tick_now;
+               sprintf( fps_display, "FPS: %d", frames_drawn );
+               frames_drawn = 0;
+       }
+       pspDebugScreenSetOffset( (int)fbp0 );
+       pspDebugScreenSetXY( 0, 0 );
+       pspDebugScreenPrintf( fps_display );
+
+}
diff --git a/gfx.h b/gfx.h
new file mode 100644 (file)
index 0000000..35ceb15
--- /dev/null
+++ b/gfx.h
@@ -0,0 +1,27 @@
+#if !defined(GFX_H)
+#define GFX_H
+
+#include "singleton.h"
+
+class GFX: public Singleton<GFX>
+{
+       void *display_list;
+       void *fb0; //< frame buffer
+
+       int  frames_drawn;
+       char fps_display[100];
+       u32  tick_resolution;
+       u64  fps_tick_now, fps_tick_last;
+
+       public:
+       const int SCR_WIDTH=480;
+       const int SCR_HEIGHT=272;
+       const int BUF_WIDTH=512;
+
+       void init();
+       void cleanup();
+       void update_fps();
+};
+
+#endif
+
diff --git a/logger.h b/logger.h
new file mode 100644 (file)
index 0000000..28a6063
--- /dev/null
+++ b/logger.h
@@ -0,0 +1,97 @@
+#if !defined(LOGGER_H)
+#define LOGGER_H
+
+#include <string>
+#include <iostream>
+#include <sstream>
+#include <ctime>
+#include "Singleton.h"
+
+class Logger: public Singleton<Logger>
+{
+       public:
+               enum log_level{
+                       OFF = -1,
+                       CRITICAL=0,
+                       ERROR=1,
+                       WARNING=2,
+                       INFO=3,
+                       DEBUG=4,
+                       TRACE=5
+               };
+
+               Logger(): 
+                       out(std::cerr), 
+                       current_log_level(WARNING),
+                       log_start_time(time(NULL))
+               {
+                       //log_start_time = time(NULL);
+                       out << "Starting log at " << static_cast<long>(log_start_time) << std::endl;
+               }
+
+               //void setOutput(std::ostream os) { out = os; }
+
+               void log(log_level level, std::string str)
+               {
+                       if (level <= current_log_level)
+                               out << "[" << difftime(time(NULL), log_start_time) << "] " << str << std::endl;
+               }
+               
+               #define LOGFUNC1(func, tag) template <typename T1> void func(T1 p1) \
+               { std::stringstream str; str << p1;     log(tag, str.str()); }
+               #define LOGFUNC2(func, tag) template <typename T1, typename T2> void func(T1 p1, T2 p2) \
+               { std::stringstream str; str << p1 << p2; log(tag, str.str()); }
+               #define LOGFUNC3(func, tag) template <typename T1, typename T2, typename T3> \
+               void func(T1 p1, T2 p2, T3 p3) \
+               { std::stringstream str; str << p1 << p2 << p3; log(tag, str.str()); }
+               #define LOGFUNC4(func, tag) template <typename T1, typename T2, typename T3, typename T4> \
+               void func(T1 p1, T2 p2, T3 p3, T4 p4) \
+               { std::stringstream str; str << p1 << p2 << p3 << p4; log(tag, str.str()); }
+               #define LOGFUNC5(func, tag) template <typename T1, typename T2, typename T3, typename T4, typename T5> \
+               void func(T1 p1, T2 p2, T3 p3, T4 p4, T5 p5) \
+               { std::stringstream str; str << p1 << p2 << p3 << p4 << p5; log(tag, str.str()); }
+               #define LOGFUNC6(func, tag) template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6> \
+               void func(T1 p1, T2 p2, T3 p3, T4 p4, T5 p5, T6 p6) \
+               { std::stringstream str; str << p1 << p2 << p3 << p4 << p5 << p6; log(tag, str.str()); }
+               #define LOGFUNC7(func, tag) template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7> \
+               void func(T1 p1, T2 p2, T3 p3, T4 p4, T5 p5, T6 p6, T7 p7) \
+               { std::stringstream str; str << p1 << p2 << p3 << p4 << p5 << p6 << p7; log(tag, str.str()); }
+               #define LOGFUNC8(func, tag) template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8> \
+               void func(T1 p1, T2 p2, T3 p3, T4 p4, T5 p5, T6 p6, T7 p7, T8 p8) \
+               { std::stringstream str; str << p1 << p2 << p3 << p4 << p5 << p6 << p7 << p8; log(tag, str.str()); }
+               #define LOGFUNC9(func, tag) template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9> \
+               void func(T1 p1, T2 p2, T3 p3, T4 p4, T5 p5, T6 p6, T7 p7, T8 p8, T9 p9) \
+               { std::stringstream str; str << p1 << p2 << p3 << p4 << p5 << p6 << p7 << p8 << p9; log(tag, str.str()); }
+               #define LOGFUNC10(func, tag) template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9, typename T10> \
+               void func(T1 p1, T2 p2, T3 p3, T4 p4, T5 p5, T6 p6, T7 p7, T8 p8, T9 p9, T10 p10) \
+               { std::stringstream str; str << p1 << p2 << p3 << p4 << p5 << p6 << p7 << p8 << p9 << p10; log(tag, str.str()); }
+
+               #define LOGFUNC(func, tag) \
+                       LOGFUNC1(func, tag) \
+                       LOGFUNC2(func, tag) \
+                       LOGFUNC3(func, tag) \
+                       LOGFUNC4(func, tag) \
+                       LOGFUNC5(func, tag) \
+                       LOGFUNC6(func, tag) \
+                       LOGFUNC7(func, tag) \
+                       LOGFUNC8(func, tag) \
+                       LOGFUNC9(func, tag) \
+                       LOGFUNC10(func, tag)
+
+               LOGFUNC(critical, CRITICAL)
+               LOGFUNC(error,    ERROR)
+               LOGFUNC(warning,  WARNING)
+               LOGFUNC(info,     INFO)
+               LOGFUNC(debug,    DEBUG)
+               LOGFUNC(trace,    TRACE)
+
+               void set_log_level (log_level level) { current_log_level = level; }
+       private:
+               std::ostream& out;
+               log_level current_log_level;
+               time_t log_start_time;
+};
+
+#define logger Logger::Instance()
+
+#endif
diff --git a/singleton.h b/singleton.h
new file mode 100644 (file)
index 0000000..e500f21
--- /dev/null
@@ -0,0 +1,23 @@
+// Meyers' singleton
+// from http://www.devarticles.com/c/a/Cplusplus/C-plus-plus-In-Theory-The-Singleton-Pattern-Part-2/1/
+#if !defined(SINGLETON_H)
+#define SINGLETON_H
+
+template <typename T>
+class Singleton
+{
+       public:
+       static T& Instance() 
+       {
+               static T _instance;
+               return _instance;
+       }
+
+       protected:
+               Singleton() {}          // ctor hidden
+               virtual ~Singleton() {}          // dtor hidden
+               Singleton(Singleton const&);    // copy ctor hidden
+               Singleton& operator=(Singleton const&);  // assign op hidden
+};
+
+#endif