落書きノート

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

C/C++とSDL2を利用してビジュアルノベルゲーム風のプログラム作成

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

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
void render();
void stringSettings();
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 = 48;
TTF_Font *gFont;
const std::string gMessage = "aiueoaieuoaieuoaieuo";
SDL_Rect gMessageRect;
SDL_Rect gRenderStringPosition;

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 stringSettings()
{
  int iw, ih;
  SDL_Color color = { 255, 255, 255, 128 };
  gTextSurface = TTF_RenderText_Solid(gFont,
                                      gMessage.c_str(),
                                      color);
  gStringTexture = SDL_CreateTextureFromSurface(gRenderer, gTextSurface);
  SDL_Rect drawRect = { 35, 330, gTextSurface->w, gTextSurface->h };
  gRenderStringPosition = drawRect;
}

bool init()
{
  bool success = true;
  if(SDL_Init(SDL_INIT_VIDEO) < 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();
          stringSettings();
          render();
        }
      }
    }
  }
  
  close();
  
  return 0;
}

f:id:kh2n:20170628214521p:plain