Author: mesyeti <mesyeti@mesyeti.uk>
resizing is kinda working
source/app.c | 10 +++++++++- source/backends/soft.c | 17 ++++++++++++++++- source/buffer.c | 30 +++++++++++++++++++++++++++++- source/buffer.h | 1 + source/screen.c | 12 ++++++++++++ source/screen.h | 1 +
diff --git a/source/app.c b/source/app.c index ed4ce6eed5076ca7154a7c24cb3bd014cc3d918b..daaaa27944d23383c19498aec1ab46fe35c0d818 100644 --- a/source/app.c +++ b/source/app.c @@ -40,7 +40,15 @@ Event e; while (Event_Poll(&e)) { switch (e.type) { - case EVENT_QUIT: app.running = false; break; + case EVENT_QUIT: { + app.running = false; + break; + } + case EVENT_WINDOW_RESIZE: { + Backend_OnWindowResize(); + Screen_OnWindowResize(); + break; + } default: { for (size_t i = 0; i < app.tabNum; ++ i) { Pane_HandleEvent(&app.tabs[i], &e); diff --git a/source/backends/soft.c b/source/backends/soft.c index 33709c93f12e00e613b13a101abe2c91f93aa7c4..33bc739521d929772494125104b5e4e5ef5254d8 100644 --- a/source/backends/soft.c +++ b/source/backends/soft.c @@ -132,7 +132,22 @@ return (Vec2) {tex->width, tex->height}; } static void OnWindowResize(void) { - + state.redraw = true; + state.bufferW = video.windows[0].width; + state.bufferH = video.windows[0].height; + state.buffer = SafeRealloc(state.buffer, state.bufferW * state.bufferH * sizeof(uint32_t)); + + SDL_DestroyTexture(state.texture); + + state.texture = SDL_CreateTexture( + state.renderer, SDL_PIXELFORMAT_ABGR8888, + SDL_TEXTUREACCESS_STREAMING, video.windows[0].width, video.windows[0].height + ); + + if (state.texture == NULL) { + fprintf(stderr, "Failed to create texture: %s\n", SDL_GetError()); + exit(1); + } } static void FillRect(int x, int y, int w, int h, Colour colour) { diff --git a/source/buffer.c b/source/buffer.c index aa15bf3ea745cae39d25998b58710d9636633f17..c4c5ef8954d3fc8441343baec686a1123c153324 100644 --- a/source/buffer.c +++ b/source/buffer.c @@ -116,7 +116,13 @@ int sx = ix + clip.x; int sy = iy + clip.y; - *Buffer_Get(dest, x + ix, y + iy) = *Buffer_Get(src, sx, sy); + BufCell* out = Buffer_Get(dest, x + ix, y + iy); + BufCell* in = Buffer_Get(src, sx, sy); + + if (!out || !in) continue; + + // *Buffer_Get(dest, x + ix, y + iy) = *Buffer_Get(src, sx, sy); + *out = *in; } } } @@ -126,3 +132,25 @@ for (int i = 0; i < len; ++ i) { Buffer_Set(buf, x + i, y, cell); } } + +void Buffer_Resize(Buffer* buf, int w, int h) { + BufCell* new = SafeMalloc(w * h * sizeof(BufCell)); + + for (int x = 0; x < w; ++ x) { + for (int y = 0; y < h; ++ y) { + BufCell* old = Buffer_Get(buf, x, y); + + if (!old) { + new[(y * w) + x] = BufAttr_BlankCell(); + } + else { + new[(y * w) + x] = *old; + } + } + } + + free(buf->buf); + buf->buf = new; + buf->width = w; + buf->height = h; +} diff --git a/source/buffer.h b/source/buffer.h index 8e8c32d480e43aa68fa85b1bd19a94bf289b7fd7..488409e0813b2d56df4b25866fd5d55c6a373407 100644 --- a/source/buffer.h +++ b/source/buffer.h @@ -51,5 +51,6 @@ void Buffer_Clear(Buffer* buf); void Buffer_Print(Buffer* buf, int x, int y, BufAttr attr, const char* str); void Buffer_Blit(Buffer* dest, Buffer* src, int x, int y, Rect clip); void Buffer_HLine(Buffer* buf, int x, int y, int len, BufCell cell); +void Buffer_Resize(Buffer* buf, int w, int h); #endif diff --git a/source/screen.c b/source/screen.c index 8155d07e2a0200f3bb47530e224741114784de5c..e060db10f102b21d66ea798e5bbfd94aec3ffc33 100644 --- a/source/screen.c +++ b/source/screen.c @@ -53,6 +53,18 @@ return !Buffer_CellEq(*a, *b); } +void Screen_OnWindowResize() { + int cw; + int ch; + Font_CharSize(&screen.font, &cw, &ch); + + int newW = video.windows[0].width / cw; + int newH = video.windows[0].height / ch; + + Buffer_Resize(&screen.buffer, newW, newH); + Buffer_Resize(&screen.old, newW, newH); +} + void Screen_Render(void) { Backend_Render(); diff --git a/source/screen.h b/source/screen.h index acd3e07ce8a6eb3c06a9e7cc8cc2da351508d8f3..8b4ea2faf4e400bceb000200458df0ef810124a2 100644 --- a/source/screen.h +++ b/source/screen.h @@ -16,6 +16,7 @@ void Screen_Init(void); void Screen_Free(void); ColourRGB Screen_GetColour(bool fg, uint8_t mode, BufCol col); bool Screen_CellChanged(int x, int y); +void Screen_OnWindowResize(); void Screen_Render(void); #endif