Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PCMockup: Add WindowGrid #46

Merged
merged 3 commits into from
Nov 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ set(sources_pcmockup
pcmockup/pebblewindow.c
pcmockup/debugwindow.c
pcmockup/debugwindowset.c
pcmockup/windowgrid.c
)
assign_source_group(${sources_pcmockup})

Expand Down
10 changes: 3 additions & 7 deletions pcmockup/debugwindowset.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct DebugWindowSet
Renderer* renderer;
};

DebugWindowSet* debugWindowSet_init(SDL_Rect mainWindowBounds, Renderer* renderer)
DebugWindowSet* debugWindowSet_init(const WindowGrid* grid, Renderer* renderer)
{
DebugWindowSet* me = (DebugWindowSet*)malloc(sizeof(DebugWindowSet));
if (me == NULL)
Expand All @@ -28,22 +28,18 @@ DebugWindowSet* debugWindowSet_init(SDL_Rect mainWindowBounds, Renderer* rendere
}
memset(me->windows, 0, sizeof(DebugWindow*) * me->count);

SDL_Rect curBounds = mainWindowBounds;
curBounds.x += mainWindowBounds.w + WINDOW_GAP;

for (int i = 0; i < me->count; i++)
{
me->windows[i] = debugWindow_init(
curBounds, i,
windowGrid_getSingleBounds(grid, -1 - i),
i,
renderer_getDebugName(renderer, i)
);
if (me->windows[i] == NULL)
{
debugWindowSet_free(me);
return NULL;
}

curBounds.y += mainWindowBounds.h + WINDOW_GAP;
}
#endif

Expand Down
20 changes: 18 additions & 2 deletions pcmockup/pcmockup.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,21 @@ int main(int argc, char* argv[])
if (renderer == NULL)
return -1;

SDL_DisplayMode displayMode;
SDL_GetCurrentDisplayMode(0, &displayMode);
WindowGrid windowGrid;
windowGrid.windowCount = 1 + renderer_getDebugCount(renderer);
windowGrid.totalSize = GSize(displayMode.w, displayMode.h);

PebbleWindow* pebbleWindow = pebbleWindow_init(
GSize(START_WINDOW_WIDTH, START_WINDOW_HEIGHT),
windowGrid_getSingleBounds(&windowGrid, 0),
GSize(RENDERER_WIDTH, RENDERER_HEIGHT)
);
if (pebbleWindow == NULL)
return -1;

DebugWindowSet* debugWindowSet = debugWindowSet_init(
pebbleWindow_getBounds(pebbleWindow),
&windowGrid,
renderer
);
if (debugWindowSet == NULL)
Expand Down Expand Up @@ -132,3 +138,13 @@ SDL_Rect findBestFit(SDL_Rect src, float dstAspect)
}
return dst;
}

SDL_Rect padRect(SDL_Rect rect, GSize amount)
{
return (SDL_Rect) {
rect.x + amount.w / 2,
rect.y + amount.h / 2,
rect.w - amount.w,
rect.h - amount.h
};
}
13 changes: 11 additions & 2 deletions pcmockup/pcmockup.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@
#include "../renderer/renderer.h"

SDL_Rect findBestFit(SDL_Rect target, float aspect);
SDL_Rect padRect(SDL_Rect rect, GSize amount);

typedef struct WindowGrid
{
int windowCount;
GSize totalSize;
} WindowGrid;
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(GSize windowSize, GSize pebbleSize);
PebbleWindow* pebbleWindow_init(SDL_Rect initialBounds, GSize pebbleSize);
void pebbleWindow_free(PebbleWindow* window);
void pebbleWindow_update(PebbleWindow* window);
SDL_Rect pebbleWindow_getBounds(PebbleWindow* window);
Expand All @@ -22,7 +31,7 @@ void debugWindow_handleEvent(DebugWindow* window, const SDL_Event* ev);
const DebugInfo* debugWindow_getDebugInfo(DebugWindow* window);

typedef struct DebugWindowSet DebugWindowSet;
DebugWindowSet* debugWindowSet_init(SDL_Rect mainWindowBounds, Renderer* renderer);
DebugWindowSet* debugWindowSet_init(const WindowGrid* grid, Renderer* renderer);
void debugWindowSet_free(DebugWindowSet* set);
void debugWindowSet_update(DebugWindowSet* set);
void debugWindowSet_handleUpdate(DebugWindowSet* set, const SDL_Event* ev);
Expand Down
2 changes: 1 addition & 1 deletion pcmockup/pebble.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ typedef struct GPoint
int16_t x;
int16_t y;
} GPoint;
#define GPoint(x, y) (__makeGPoint((x), (y))
#define GPoint(x, y) (__makeGPoint((x), (y)))
GPoint __makeGPoint(int16_t x, int16_t y);

typedef struct GSize
Expand Down
6 changes: 3 additions & 3 deletions pcmockup/pebblewindow.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ struct PebbleWindow
GSize pebbleSize;
};

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

this->window = SDL_CreateWindow("PebbleOfDoom - PCMockup",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
windowSize.w, windowSize.h,
initialBounds.x, initialBounds.y,
initialBounds.w, initialBounds.h,
SDL_WINDOW_RESIZABLE);
if (this->window == NULL)
{
Expand Down
63 changes: 63 additions & 0 deletions pcmockup/windowgrid.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "pcmockup.h"

/* WindowGrid is a grid-layout system based on
* alternating incremented grid dimensions, eg:
*
* 2*2 3*2 3*3
* xx xxx xxx
* xx -> xxx -> xxx
* xxx
*
* Thus we have a grid which can be adjusted
* dynamically to answer growing window demand.
* There are a few important values:
*
* Grid size index
* i 0 1 2 3 4 5 6 7 8
* Base size s = i / 2 + 1
* s 1 1 2 2 3 3 4 4 5
* Grid width w = s + (i % 2)
* w 1 2 2 3 3 4 4 5 5
* Grid height h = s
* w 1 1 2 2 3 3 4 4 5
* Maximum number of windows m = w * h
* m 1 2 4 6 9 12 16 20 25
*
* As further demonstration all possible window
* counts per size index
* 1 2 3 5 7 10 13 17 21
* 4 6 8 11 14 18 22
* 9 12 15 19 23
* 16 20 24
* 25
*/

#define WINDOWGRID_PADDING (GSize(32, 64))

GSize windowGrid_getGridSize(const WindowGrid* grid)
{
int i = 0, w, h;
do {
h = i / 2;
w = h + (i % 2);
i++;
} while(w * h <= grid->windowCount);
return GSize(w, h);
}

SDL_Rect windowGrid_getSingleBounds(const WindowGrid* grid, int windowI)
{
GSize gridSize = windowGrid_getGridSize(grid);
SDL_Rect totalBounds = { 0, 0, grid->totalSize.w, grid->totalSize.h };
totalBounds = findBestFit(totalBounds, (float)gridSize.w / gridSize.h);
if (windowI < 0)
windowI = gridSize.w * gridSize.h + windowI;

SDL_Rect bounds;
bounds.w = totalBounds.w / gridSize.w;
bounds.h = totalBounds.h / gridSize.h;
bounds.x = totalBounds.x + bounds.w * (windowI % gridSize.w);
bounds.y = totalBounds.y + bounds.h * (windowI / gridSize.w);

return padRect(bounds, WINDOWGRID_PADDING);
}