落書きノート

ふと自分が気になった事を書いてます

C/C++でビジュアルノベルゲーム風のプログラム作成終了

結果は失敗に終わりました。仕様が変更されて融通が効きません。文字の表示のラッピング(折り返し表示)が出来ない仕様になりましたのでここで諦めました。調べてみると以前は出来てたみたいですが、まあここで切り上げておきます。趣味プログラムで考え過ぎも良くないですし。

#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
void render();
void messageWindowSettings();
void drawStringSettings();
bool init();
bool loadMedia();
void close();
SDL_Window *gWindow = NULL;
SDL_Renderer *gRenderer = NULL;
SDL_Surface *gHelloWorld = NULL;
SDL_Texture *gHelloWorldTexture = NULL;
SDL_Surface *gTextSurface = NULL;
SDL_Texture *gStringTexture = NULL;
SDL_Event event;
bool done = false;
const std::string gfontName = "TakaoGothic.ttf";
const int gfontSize = 32;
TTF_Font *gFont;
SDL_Rect gRenderStringPosition;
SDL_Rect gMessageRect;

void render()
{
  SDL_RenderClear(gRenderer);
  SDL_RenderCopy(gRenderer, gHelloWorldTexture, NULL, NULL);
  SDL_SetRenderDrawBlendMode(gRenderer, SDL_BLENDMODE_BLEND);
  SDL_SetRenderDrawColor(gRenderer, 0, 0, 0, 128);
  SDL_RenderFillRect(gRenderer, &gMessageRect);
  SDL_RenderCopy(gRenderer, gStringTexture, NULL, &gRenderStringPosition);
  SDL_RenderPresent(gRenderer);
}

void messageWindowSettings() {
  SDL_Rect messageRect = { 30, 300, SCREEN_WIDTH - 60, SCREEN_HEIGHT - 320 };
  gMessageRect = messageRect;
}

void drawStringSettings()
{
  std::string str;
  std::ifstream ifs("test.txt");
  if(!ifs) {
    std::cerr << "File Read Error!" << std::endl;
    std::exit(EXIT_FAILURE);
  }
  getline(ifs, str);
  gTextSurface = TTF_RenderUTF8_Blended(gFont,
                                        str.c_str(),
                                        (SDL_Color) { 255, 255, 255, 128 });
  gStringTexture = SDL_CreateTextureFromSurface(gRenderer, gTextSurface);
  gRenderStringPosition = (SDL_Rect) { 40, 310, gTextSurface->w, gTextSurface->h };
}

bool init()
{
  bool success = true;
  if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) {
    printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
    success = false;
  } else {
    SDL_CreateWindowAndRenderer(SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_RESIZABLE, &gWindow, &gRenderer);
    if(gWindow == NULL || gRenderer == NULL) {
      printf( "Window and Renderer could not be created! SDL_Error: %s\n", SDL_GetError() );
      success = false;
    }
  }

  SDL_SetWindowTitle(gWindow, "Test Game");
  TTF_Init();
  gFont = TTF_OpenFont(gfontName.c_str(), gfontSize);
  
  return success;
}

bool loadMedia()
{
  bool success = true;
  gHelloWorld = SDL_LoadBMP("./hello_world.bmp");
  if( gHelloWorld == NULL )
    {
      printf( "Unable to load image %s! SDL Error: %s\n", "02_getting_an_image_on_the_screen/hello_world.bmp", SDL_GetError() );
      success = false;
    }
  gHelloWorldTexture = SDL_CreateTextureFromSurface(gRenderer, gHelloWorld);
  if(gHelloWorldTexture == NULL)
    {
      printf("Unable to load Texture! SDL Error: %s\n", SDL_GetError());
      success = false;
    }
  return success;
}

void close()
{
  SDL_FreeSurface(gHelloWorld);
  SDL_FreeSurface(gTextSurface);
  SDL_DestroyTexture(gHelloWorldTexture);
  SDL_DestroyTexture(gStringTexture);
  SDL_DestroyRenderer(gRenderer);
  SDL_DestroyWindow(gWindow);
  SDL_Quit();
  TTF_CloseFont(gFont);
  TTF_Quit();
}

int main( int argc, char* args[] )
{
  if(!init()) {
    printf( "Failed to initialize!\n" );
  } else {
    if(!loadMedia()) {
      printf( "Failed to load media!\n" );
    }
    else {
      while(!done) {
        while(SDL_PollEvent(&event)) {
          switch(event.type) {
          case SDL_KEYDOWN:
            if(event.key.keysym.sym == SDLK_ESCAPE) {
              done = true;
            }
            break;
          case SDL_QUIT:
            done = true;
            break;
          }
          messageWindowSettings();
          drawStringSettings();
          render();
        }
      }
    }
  }
  
  close();
  
  return 0;
}

macOSでの使い方(失敗してますけど。笑)

> brew install sdl2 sdl2_ttf
> g++ menu.cpp -o menu -I/usr/local/include -L/usr/local/lib -lSDL2 -lSDL2_ttf

f:id:kh2n:20170629184413p:plain