From f8307b0b9e5609e6ab93cb1d1197eb45ee145903 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=E8=AB=8F=E8=A8=AA=E5=AD=90?= Date: Sun, 7 Apr 2024 15:16:52 +0900 Subject: [PATCH] =?utf8?q?=E7=94=BB=E9=9D=A2=E3=82=A2=E3=82=B9=E3=83=9A?= =?utf8?q?=E3=82=AF=E3=83=88=E6=AF=94=E3=83=AD=E3=83=83=E3=82=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + main.c | 58 ++++++++++++++++++++++++++++++---------------------- 2 files changed, 35 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 093b3e2..3d93ca0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # 0.4.0 * URLから画像ファイルを開ける様に * GPLv2→BSD2clouseライセンスに変更 +* 画面アスペクト比ロック # 0.3.0 * 負荷をすくなくするために画面の更新を減らした (たかしさん) diff --git a/main.c b/main.c index 3a6f7fc..4de0f38 100644 --- a/main.c +++ b/main.c @@ -10,6 +10,11 @@ SDL_Window* window = NULL; SDL_Renderer* renderer = NULL; SDL_Texture* texture = NULL; bool quit = false; +float aspectRatio; +int imgWidth; +int imgHeight; +int screenWidth; +int screenHeight; bool dlfile(const char* url, const char* filename) { CURL* curl = curl_easy_init(); @@ -44,22 +49,32 @@ void windowevent(SDL_Event e) { int newHeight = e.window.data2; // 縦横比を変わらずに新しい大きさの算数 - /* float newAspectRatio = (float)newWidth / newHeight; */ + float newAspectRatio = (float)newWidth / newHeight; int scaledWidth, scaledHeight; - /* if (newAspectRatio > aspectRatio) { */ + if (newAspectRatio != aspectRatio) { // 画像よりウィンドウの方が広い場合 - scaledHeight = newHeight; - scaledWidth = newWidth; - //= (int)(scaledHeight * aspectRatio); - /* } else { */ - /* // 画像よりウィンドウの方が高い場合 */ - /* scaledWidth = newWidth; */ - /* scaledHeight = (int)(scaledWidth / aspectRatio); */ - /* } */ + scaledHeight = newHeight; + scaledWidth = (int)(scaledHeight * aspectRatio); + } else { + // 画像よりウィンドウの方が高い場合 + scaledWidth = newWidth; + scaledHeight = newHeight; + } + + // 画像は50x50以下じゃ駄目 + int minWidth = 50; + int minHeight = 50; + if (scaledWidth < minWidth) scaledWidth = minWidth; + if (scaledHeight < minHeight) scaledHeight = minHeight; + + // 大きすぎの場合もふざけんな + if (scaledWidth >= (screenWidth-20)) scaledWidth = screenWidth-20; + if (scaledHeight >= (screenHeight-20)) scaledWidth = screenHeight-20; // テキスチャーのれんダーリングサイズの設定 - SDL_Rect renderQuad = { (newWidth - scaledWidth) / 2, (newHeight - scaledHeight) / 2, scaledWidth, scaledHeight }; + SDL_Rect renderQuad = { imgWidth, imgHeight, scaledWidth, scaledHeight }; SDL_RenderCopy(renderer, texture, NULL, &renderQuad); + SDL_SetWindowSize(window, scaledWidth, scaledHeight); } else if (e.type == SDL_WINDOWEVENT && e.window.event == SDL_WINDOWEVENT_EXPOSED) { // 再描画が必要な場合 @@ -120,25 +135,20 @@ int main(int argc, char* argv[]) { } // 画像の大きさの受取 - int imgWidth = imgsurface->w; - int imgHeight = imgsurface->h; - /* float aspectRatio = (float)imgWidth / imgHeight; */ + imgWidth = imgsurface->w; + imgHeight = imgsurface->h; + aspectRatio = (float)imgWidth / imgHeight; // 画面の大きさの受取 SDL_DisplayMode DM; SDL_GetCurrentDisplayMode(0, &DM); - int screenWidth = DM.w; - int screenHeight = DM.h; - - // 画像は50x50以下じゃ駄目 - int minWidth = 50; - int minHeight = 50; - if (imgWidth < minWidth) imgWidth = minWidth; - if (imgHeight < minHeight) imgHeight = minHeight; + screenWidth = DM.w; + screenHeight = DM.h; + printf("%d, %d, %.2f\n", DM.w, DM.h, aspectRatio); // ウィンドウの大きさの設定 - int windowWidth = (imgWidth > screenWidth) ? screenWidth : imgWidth; - int windowHeight = (imgHeight > screenHeight) ? screenHeight : imgHeight; + int windowWidth = (imgWidth >= (screenWidth-20)) ? screenWidth-20 : imgWidth; + int windowHeight = (imgHeight >= (screenHeight-20)) ? screenHeight-20 : imgHeight; // ウィンドウの作成 SDL_SetHint(SDL_HINT_VIDEO_WINDOW_SHARE_PIXEL_FORMAT, "1"); -- 2.43.0