画面アスペクト比ロック

このコミットが含まれているのは:
守矢諏訪子 2024-04-07 15:16:52 +09:00
コミット f8307b0b9e
2個のファイルの変更35行の追加24行の削除

ファイルの表示

@ -1,6 +1,7 @@
# 0.4.0
* URLから画像ファイルを開ける様に
* GPLv2→BSD2clouseライセンスに変更
* 画面アスペクト比ロック
# 0.3.0
* 負荷をすくなくするために画面の更新を減らした (たかしさん)

58
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");