Skip to content

Commit

Permalink
Cleanup+gyro visual
Browse files Browse the repository at this point in the history
  • Loading branch information
igor725 committed Mar 29, 2024
1 parent dc9bd12 commit bb56eb1
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 184 deletions.
15 changes: 14 additions & 1 deletion input/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ bool Controller::Init(int controllerUserID) {

scePadSetLightBar(this->pad, &this->padColors[this->currPadColor]);
scePadGetControllerInformation(this->pad, &padInfo);

if (scePadSetMotionSensorState(this->pad, true) != ORBIS_OK) {
DEBUGLOG << "[DEBUG] [ERROR] Failed to enable motion sensor!";
}

return true;
}

Expand Down Expand Up @@ -181,8 +186,16 @@ int Controller::ReadFingers(OrbisPadTouch **fingers) {
return this->padData.touch.fingers;
}

int Controller::GetToucPadResolution(int *w, int *h) {
int Controller::GetTouchPadResolution(int *w, int *h) {
if (w) *w = padInfo.touchResolutionX;
if (h) *h = padInfo.touchResolutionY;
return 0;
}

void Controller::ResetOrientation() {
scePadResetOrientation(this->pad);
}

void Controller::ReadGyro(vec_float4* quat) {
if (quat) *quat = this->padData.quat;
}
4 changes: 3 additions & 1 deletion input/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ class Controller
bool DpadLeftPressed();
bool TouchpadPressed();
int ReadFingers(OrbisPadTouch **fingers);
int GetToucPadResolution(int *w, int *h);
int GetTouchPadResolution(int *w, int *h);
void ReadSticks(float *leftx, float *lefty, float *rightx, float *righty);
void ReadGyro(vec_float4* data);
void ResetOrientation();
OrbisPadColor NextColor();
};

Expand Down
70 changes: 35 additions & 35 deletions input/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ Scene2D::Scene2D(int w, int h, int pixelDepth)
this->width = w;
this->height = h;
this->depth = pixelDepth;

this->frameBufferSize = this->width * this->height * this->depth;
}

bool Scene2D::Init(size_t memSize, int numFrameBuffers)
{
int rc;

this->video = sceVideoOutOpen(ORBIS_VIDEO_USER_MAIN, ORBIS_VIDEO_OUT_BUS_MAIN, 0, 0);
this->videoMem = NULL;

Expand All @@ -29,7 +29,7 @@ bool Scene2D::Init(size_t memSize, int numFrameBuffers)
DEBUGLOG << "Failed to open a video out handle: " << std::string(strerror(errno));
return false;
}

#ifdef GRAPHICS_USES_FONT
// Load freetype
rc = sceSysmoduleLoadModule(0x009A);
Expand All @@ -49,36 +49,36 @@ bool Scene2D::Init(size_t memSize, int numFrameBuffers)
return false;
}
#endif

if(!initFlipQueue())
{
DEBUGLOG << "Failed to initialize flip queue: " << std::string(strerror(errno));
return false;
}

if(!allocateVideoMem(memSize, 0x200000))
{
DEBUGLOG << "Failed to allocate video memory: " << std::string(strerror(errno));
return false;
}

if(!allocateFrameBuffers(numFrameBuffers))
{
DEBUGLOG << "Failed to allocate frame buffers: " << std::string(strerror(errno));
return false;
}

sceVideoOutSetFlipRate(this->video, 0);
return true;
}

bool Scene2D::initFlipQueue()
{
int rc = sceKernelCreateEqueue(&flipQueue, "homebrew flip queue");

if(rc < 0)
return false;

sceVideoOutAddFlipEvent(flipQueue, this->video, 0);
return true;
}
Expand All @@ -87,14 +87,14 @@ bool Scene2D::allocateFrameBuffers(int num)
{
// Allocate frame buffers array
this->frameBuffers = new char*[num];

// Set the display buffers
for(int i = 0; i < num; i++)
this->frameBuffers[i] = this->allocateDisplayMem(frameBufferSize);

// Set SRGB pixel format
sceVideoOutSetBufferAttribute(&this->attr, 0x80000000, 1, 0, this->width, this->height, this->width);

// Register the buffers to the video handle
return (sceVideoOutRegisterBuffers(this->video, 0, (void **)this->frameBuffers, num, &this->attr) == 0);
}
Expand All @@ -111,32 +111,32 @@ char *Scene2D::allocateDisplayMem(size_t size)
bool Scene2D::allocateVideoMem(size_t size, int alignment)
{
int rc;

// Align the allocation size
this->directMemAllocationSize = (size + alignment - 1) / alignment * alignment;

// Allocate memory for display buffer
rc = sceKernelAllocateDirectMemory(0, sceKernelGetDirectMemorySize(), this->directMemAllocationSize, alignment, 3, &this->directMemOff);

if(rc < 0)
{
this->directMemAllocationSize = 0;
return false;
}

// Map the direct memory
rc = sceKernelMapDirectMemory(&this->videoMem, this->directMemAllocationSize, 0x33, 0, this->directMemOff, alignment);

if(rc < 0)
{
sceKernelReleaseDirectMemory(this->directMemOff, this->directMemAllocationSize);

this->directMemOff = 0;
this->directMemAllocationSize = 0;

return false;
}

// Set the stack pointer to the beginning of the buffer
this->videoMemSP = (uintptr_t)this->videoMem;
return true;
Expand All @@ -146,13 +146,13 @@ void Scene2D::deallocateVideoMem()
{
// Free the direct memory
sceKernelReleaseDirectMemory(this->directMemOff, this->directMemAllocationSize);

// Zero out meta data
this->videoMem = 0;
this->videoMemSP = 0;
this->directMemOff = 0;
this->directMemAllocationSize = 0;

// Free the frame buffer array
delete this->frameBuffers;
this->frameBuffers = 0;
Expand All @@ -172,21 +172,21 @@ void Scene2D::FrameWait(int frameID)
{
OrbisKernelEvent evt;
int count;

// If the video handle is not initialized, bail out. This is mostly a failsafe, this should never happen.
if(this->video == 0)
return;

for(;;)
{
OrbisVideoOutFlipStatus flipStatus;

// Get the flip status and check the arg for the given frame ID
sceVideoOutGetFlipStatus(video, &flipStatus);

if(flipStatus.flipArg == frameID)
break;

// Wait on next flip event
if(sceKernelWaitEqueue(this->flipQueue, &evt, 1, &count, 0) != 0)
break;
Expand All @@ -210,17 +210,17 @@ void Scene2D::FrameBufferClear()
bool Scene2D::InitFont(FT_Face *face, const char *fontPath, int fontSize)
{
int rc;

rc = FT_New_Face(this->ftLib, fontPath, 0, face);

if(rc < 0)
return false;

rc = FT_Set_Pixel_Sizes(*face, 0, fontSize);

if(rc < 0)
return false;

return true;
}
#endif
Expand All @@ -234,18 +234,18 @@ void Scene2D::DrawPixel(int x, int y, Color color)
{
// Get pixel location based on pitch
int pixel = (y * this->width) + x;

// Encode to 24-bit color
uint32_t encodedColor = 0x80000000 + (color.r << 16) + (color.g << 8) + color.b;

// Draw to the frame buffer
((uint32_t *)this->frameBuffers[this->activeFrameBufferIdx])[pixel] = encodedColor;
}

void Scene2D::DrawRectangle(int x, int y, int w, int h, Color color)
{
int xPos, yPos;

// Draw row-by-row, column-by-column
for(yPos = y; yPos < y + h; yPos++)
{
Expand All @@ -262,10 +262,10 @@ void Scene2D::DrawText(char *txt, FT_Face face, int startX, int startY, Color bg
int rc;
int xOffset = 0;
int yOffset = 0;

// Get the glyph slot for bitmap and font metrics
FT_GlyphSlot slot = face->glyph;

// Iterate each character of the text to write to the screen
for(int n = 0; n < strlen(txt); n++)
{
Expand Down
22 changes: 11 additions & 11 deletions input/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,25 @@ class Scene2D
#ifdef GRAPHICS_USES_FONT
FT_Library ftLib;
#endif

int width;
int height;
int depth;
int video;

off_t directMemOff;
size_t directMemAllocationSize;

uintptr_t videoMemSP;
void *videoMem;

char **frameBuffers;
OrbisKernelEqueue flipQueue;
OrbisVideoOutBufferAttribute attr;

int frameBufferSize;
int frameBufferCount;

int activeFrameBufferIdx;

bool initFlipQueue();
Expand All @@ -53,20 +53,20 @@ class Scene2D
public:
Scene2D(int w, int h, int pixelDepth);
~Scene2D();

bool Init(size_t memSize, int numFrameBuffers);

void SetActiveFrameBuffer(int index);
void SubmitFlip(int frameID);

void FrameWait(int frameID);
void FrameBufferSwap();
void FrameBufferClear();
void FrameBufferFill(Color color);

void DrawPixel(int x, int y, Color color);
void DrawRectangle(int x, int y, int w, int h, Color color);

#ifdef GRAPHICS_USES_FONT
bool InitFont(FT_Face *face, const char *fontPath, int fontSize);
void DrawText(char *txt, FT_Face face, int startX, int startY, Color bgColor, Color fgColor);
Expand Down
6 changes: 3 additions & 3 deletions input/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ class Log
{
debugLogStream << funcName << ": ";
}

template <class T>
Log &operator<<(const T &v)
{
debugLogStream << v;
return *this;
}

~Log()
{
debugLogStream << std::endl;
printf("%s", debugLogStream.str().c_str());

// Clear the stream
debugLogStream.str("");
}
Expand Down
Loading

0 comments on commit bb56eb1

Please sign in to comment.