Skip to content

Commit

Permalink
pcmockup: Migrate ImageWindow and consumers to Window
Browse files Browse the repository at this point in the history
  • Loading branch information
Helco committed Nov 27, 2018
1 parent bb53984 commit 0211fa6
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 80 deletions.
7 changes: 3 additions & 4 deletions pcmockup/debugwindow.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@ SDL_Surface* createSDLSurface(int w, int h, Uint32 format)
return surface;
}

DebugWindow* debugWindow_init(SDL_Rect bounds, const DebugView* view, Renderer* podRenderer)
DebugWindow* debugWindow_init(WindowContainer* parent, SDL_Rect bounds, const DebugView* view, Renderer* podRenderer)
{
DebugWindow* me = (DebugWindow*)malloc(sizeof(DebugWindow));
if (me == NULL)
return NULL;
memset(me, 0, sizeof(DebugWindow));

me->window = imageWindow_init(view->name, (GSize) { bounds.w, bounds.h }, false);
GRect b = { { bounds.x, bounds.y }, { bounds.w, bounds.h} };
me->window = imageWindow_init(parent, view->name, b, false);
if (me->window == NULL)
{
debugWindow_free(me);
return NULL;
}
imageWindow_setInitialPosition(me->window, (GPoint) { bounds.x, bounds.y });

me->surface = createSDLSurface(bounds.w, bounds.h, SDL_PIXELFORMAT_ABGR8888);
if (me->surface == NULL)
Expand Down Expand Up @@ -89,7 +89,6 @@ void debugWindow_startUpdate(DebugWindow* me)
void debugWindow_endUpdate(DebugWindow* me)
{
imageWindow_setImageData(me->window, GSize(me->surface->w, me->surface->h), (SDL_Color*)me->surface->pixels);
imageWindow_update(me->window);
}

void debugWindow_update(DebugWindow* me)
Expand Down
3 changes: 2 additions & 1 deletion pcmockup/debugwindowset.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct DebugWindowSet
Renderer* renderer;
};

DebugWindowSet* debugWindowSet_init(const WindowGrid* grid, Renderer* renderer)
DebugWindowSet* debugWindowSet_init(WindowContainer* parent, const WindowGrid* grid, Renderer* renderer)
{
DebugWindowSet* me = (DebugWindowSet*)malloc(sizeof(DebugWindowSet));
if (me == NULL)
Expand All @@ -32,6 +32,7 @@ DebugWindowSet* debugWindowSet_init(const WindowGrid* grid, Renderer* renderer)
for (int i = 0; i < me->count; i++)
{
me->windows[i] = debugWindow_init(
parent,
windowGrid_getSingleBounds(grid, -1 - i),
&renderer_getDebugViews(renderer)[i],
renderer
Expand Down
107 changes: 44 additions & 63 deletions pcmockup/imagewindow.c
Original file line number Diff line number Diff line change
@@ -1,37 +1,48 @@
#define _CRT_NONSTDC_NO_DEPRECATE
#include "pcmockup.h"
#include <glad/glad.h>
#include "cimgui.include.h"
#include <float.h>

struct ImageWindow
{
char* title;
Window* window;
GLuint textureID;
GSize windowSize, imageSize;
GSize imageSize;
float aspect;
SDL_Rect lastContentPos;
bool isOpen, isEssential;
ImVec2 initialPosition;
bool isEssential;
float toolbarHeight;
};

const Uint32 imageWindow_SDLPixelFormat = SDL_PIXELFORMAT_ABGR8888;

ImageWindow* imageWindow_init(const char* title, GSize initialSize, bool_t isEssential)
void imageWindow_beforeUpdate(Window* me);
void imageWindow_contentUpdate(Window* me);

ImageWindow* imageWindow_init(WindowContainer* parent, const char* title, GRect initialBounds, bool_t isEssential)
{
ImageWindow* me = (ImageWindow*)malloc(sizeof(ImageWindow));
if (me == NULL)
return NULL;
memset(me, 0, sizeof(ImageWindow));

me->title = strdup(title);
if (me->title == NULL)
me->window = windowContainer_newWindow(parent, title);
if (me->window == NULL)
{
fprintf(stderr, "strdup: failure\n");
fprintf(stderr, "Could not allocate new window\n");
imageWindow_free(me);
return NULL;
}
window_setUserdata(me->window, me);
window_setInitialBounds(me->window, initialBounds);
window_setOpenState(me->window, isEssential ? WindowOpenState_Unclosable : WindowOpenState_Open);
window_setFlags(me->window,
ImGuiWindowFlags_NoScrollbar |
ImGuiWindowFlags_NoScrollWithMouse |
ImGuiWindowFlags_NoCollapse);
window_setUpdateCallbacks(me->window, (WindowUpdateCallbacks) {
.before = imageWindow_beforeUpdate,
.content = imageWindow_contentUpdate
});

glGenTextures(1, &me->textureID);
if (me->textureID == 0)
Expand All @@ -49,11 +60,8 @@ ImageWindow* imageWindow_init(const char* title, GSize initialSize, bool_t isEss
const SDL_Color black = { 255, 255, 255, 255 };
imageWindow_setImageData(me, GSize(1, 1), &black);

me->isOpen = true;
me->isEssential = isEssential;
me->windowSize = initialSize;
me->aspect = (float)initialSize.w / initialSize.h;
me->initialPosition.x = -1; // undefined initial position
me->aspect = (float)initialBounds.size.w / initialBounds.size.h;
return me;
}

Expand All @@ -63,8 +71,6 @@ void imageWindow_free(ImageWindow* me)
return;
if (me->textureID != 0)
glDeleteTextures(1, &me->textureID);
if (me->title != NULL)
free(me->title);
free(me);
}

Expand All @@ -75,13 +81,6 @@ void imageWindow_setImageData(ImageWindow* me, GSize size, const SDL_Color* data
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, size.w, size.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
me->imageSize = size;
me->aspect = (float)size.w / (float)size.h;
me->windowSize.w = max(me->windowSize.w, size.w);
me->windowSize.h = max(me->windowSize.h, size.h);
}

SDL_Rect imageWindow_getContentPos(ImageWindow* me)
{
return me->lastContentPos;
}

void imageWindow_constrainWindowSize(ImGuiSizeCallbackData* data)
Expand All @@ -103,64 +102,46 @@ void imageWindow_constrainWindowSize(ImGuiSizeCallbackData* data)
data->DesiredSize = byWidthArea > byHeightArea ? byWidth : byHeight;
}

void imageWindow_update(ImageWindow* me)
void imageWindow_beforeUpdate(Window* window)
{
if (!me->isOpen)
return;
ImageWindow* me = (ImageWindow*)window_getUserdata(window);
const ImVec2
size = { (float)me->windowSize.w, (float)me->windowSize.h },
zero = { 0, 0 },
minSize = { (float)me->imageSize.w, (float)me->imageSize.h },
maxSize = { FLT_MAX, FLT_MAX },
maxSize = { FLT_MAX, FLT_MAX };
igSetNextWindowSizeConstraints(minSize, maxSize, imageWindow_constrainWindowSize, me);
igPushStyleVarVec2(ImGuiStyleVar_WindowPadding, zero); // space between image and window border
}

void imageWindow_contentUpdate(Window* window)
{
const ImageWindow* me = (const ImageWindow*)window_getUserdata(window);
const GSize windowSize = window_getBounds(window).size;
const ImVec2
zero = { 0, 0 },
one = { 1 + FLT_EPSILON, 1 + FLT_EPSILON };
ImVec2 toolbarSize, windowPos;
one = { 1 + FLT_EPSILON, 1 + FLT_EPSILON },
size = { (float)windowSize.w, (float)windowSize.h };
const ImVec4
tintColor = { 1, 1, 1, 1 },
borderColor = { 0, 0, 0, 0 };
bool* isOpenPtr = me->isEssential
? NULL // essential windows don't get a close button
: &me->isOpen;
ImVec2 toolbarSize;

if (me->initialPosition.x >= 0 && me->initialPosition.y >= 0)
igSetNextWindowPos(me->initialPosition, ImGuiCond_Once, zero);
igSetNextWindowSizeConstraints(minSize, maxSize, imageWindow_constrainWindowSize, me);
igPushStyleVarVec2(ImGuiStyleVar_WindowPadding, zero); // space between image and window border
igBegin(me->title, isOpenPtr,
ImGuiWindowFlags_NoScrollbar |
ImGuiWindowFlags_NoScrollWithMouse |
ImGuiWindowFlags_NoCollapse);
igGetItemRectSize_nonUDT(&toolbarSize);
igGetWindowPos_nonUDT(&windowPos);
igImageButton((ImTextureID)(intptr_t)me->textureID, size, zero, one, 0, borderColor, tintColor);
igPopStyleVar(1);

me->windowSize.w = (int16_t)igGetWindowWidth();
me->windowSize.h = (int16_t)(igGetWindowHeight() - toolbarSize.y);
me->toolbarHeight = toolbarSize.y;
igEnd();

me->lastContentPos = (SDL_Rect) {
(int)(windowPos.x),
(int)(windowPos.y),
(int)me->windowSize.w,
(int)me->windowSize.h
};
}

void imageWindow_setInitialPosition(ImageWindow* me, GPoint initialPosition)
void imageWindow_toggle(ImageWindow* me, bool_t isOpen)
{
me->initialPosition = (ImVec2) {
(float)initialPosition.x,
(float)initialPosition.y
};
window_setOpenState(me->window, isOpen ? WindowOpenState_Open : WindowOpenState_Closed);
}

void imageWindow_toggle(ImageWindow* me, bool_t isOpen)
bool_t imageWindow_isOpen(ImageWindow* me)
{
me->isOpen = isOpen;
return window_getOpenState(me->window) == WindowOpenState_Open;
}

bool_t imageWindow_isOpen(ImageWindow* me)
Window* imageWindow_asWindow(ImageWindow* me)
{
return me->isOpen;
return me->window;
}
2 changes: 2 additions & 0 deletions pcmockup/pcmockup.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ PCMockup *pcmockup_init()
windowGrid.totalSize = WINDOW_START_SIZE;

me->pebbleWindow = pebbleWindow_init(
me->windowContainer,
windowGrid_getSingleBounds(&windowGrid, 0),
GSize(RENDERER_WIDTH, RENDERER_HEIGHT)
);
if (me->pebbleWindow == NULL)
return NULL;

me->debugWindowSet = debugWindowSet_init(
me->windowContainer,
&windowGrid,
me->renderer
);
Expand Down
14 changes: 6 additions & 8 deletions pcmockup/pcmockup.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ SDL_Rect padRect(SDL_Rect rect, GSize amount);

extern const Uint32 imageWindow_SDLPixelFormat;
typedef struct ImageWindow ImageWindow;
ImageWindow* imageWindow_init(const char* title, GSize initialSize, bool_t isEssential);
ImageWindow* imageWindow_init(WindowContainer* parent, const char* title, GRect initialBounds, bool_t isEssential);
void imageWindow_free(ImageWindow* me);
void imageWindow_setImageData(ImageWindow* me, GSize size, const SDL_Color* data);
SDL_Rect imageWindow_getContentPos(ImageWindow* me); // in screen coordinates
void imageWindow_update(ImageWindow* me);
void imageWindow_setInitialPosition(ImageWindow* me, GPoint initialPosition);
void imageWindow_toggle(ImageWindow* me, bool_t isOpen);
bool_t imageWindow_isOpen(ImageWindow* me);
Window* imageWindow_asWindow(ImageWindow* me);

typedef struct WindowGrid
{
Expand All @@ -28,22 +26,22 @@ GSize windowGrid_getGridSize(const WindowGrid* grid);
SDL_Rect windowGrid_getSingleBounds(const WindowGrid* grid, int windowI); // negative to select from end

typedef struct PebbleWindow PebbleWindow;
PebbleWindow* pebbleWindow_init(SDL_Rect initialBounds, GSize pebbleSize);
PebbleWindow* pebbleWindow_init(WindowContainer* parent, SDL_Rect initialBounds, GSize pebbleSize);
void pebbleWindow_free(PebbleWindow* window);
void pebbleWindow_update(PebbleWindow* window);
void pebbleWindow_update(PebbleWindow* me);
GColor* pebbleWindow_getPebbleFramebuffer(PebbleWindow* window);
ImageWindow* pebbleWindow_asImageWindow(PebbleWindow* window);

typedef struct DebugWindow DebugWindow;
DebugWindow* debugWindow_init(SDL_Rect bounds, const DebugView* debugView, Renderer* renderer);
DebugWindow* debugWindow_init(WindowContainer* parent, SDL_Rect bounds, const DebugView* debugView, Renderer* renderer);
void debugWindow_free(DebugWindow* window);
void debugWindow_update(DebugWindow* window);
void debugWindow_handleEvent(DebugWindow* window, const SDL_Event* ev);
const DebugView* debugWindow_getDebugView(const DebugWindow* window);
ImageWindow* debugWindow_asImageWindow(DebugWindow* window);

typedef struct DebugWindowSet DebugWindowSet;
DebugWindowSet* debugWindowSet_init(const WindowGrid* grid, Renderer* renderer);
DebugWindowSet* debugWindowSet_init(WindowContainer* parent, const WindowGrid* grid, Renderer* renderer);
void debugWindowSet_free(DebugWindowSet* set);
void debugWindowSet_update(DebugWindowSet* set);
void debugWindowSet_updateMenubar(DebugWindowSet* me);
Expand Down
7 changes: 3 additions & 4 deletions pcmockup/pebblewindow.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ struct PebbleWindow
GSize pebbleSize;
};

PebbleWindow* pebbleWindow_init(SDL_Rect initialBounds, GSize pebbleSize)
PebbleWindow* pebbleWindow_init(WindowContainer* parent, SDL_Rect initialBounds, GSize pebbleSize)
{
PebbleWindow* me = (PebbleWindow*)malloc(sizeof(PebbleWindow));
if (me == NULL)
return NULL;
memset(me, 0, sizeof(PebbleWindow));

me->window = imageWindow_init("Pebble screen", (GSize) { initialBounds.w, initialBounds.h }, true);
GRect b = { { initialBounds.x, initialBounds.y }, { initialBounds.w, initialBounds.h } };
me->window = imageWindow_init(parent, "Pebble screen", b, true);
if (me->window == NULL)
{
pebbleWindow_free(me);
return NULL;
}
imageWindow_setInitialPosition(me->window, (GPoint) { initialBounds.x, initialBounds.y });

me->textureData = (SDL_Color*)malloc(sizeof(SDL_Color) * pebbleSize.w * pebbleSize.h);
if (me->textureData == NULL)
Expand Down Expand Up @@ -109,7 +109,6 @@ void pebbleWindow_update(PebbleWindow* me)
{
prv_pebbleWindow_convertPebbleToTexture(me);
imageWindow_setImageData(me->window, me->pebbleSize, me->textureData);
imageWindow_update(me->window);
}

GColor* pebbleWindow_getPebbleFramebuffer(PebbleWindow* window)
Expand Down

0 comments on commit 0211fa6

Please sign in to comment.