Browse Source

update 2019-09-28_15:14:22

master
老赵 6 months ago
parent
commit
541e668be4
7 changed files with 251 additions and 2 deletions
  1. 4
    0
      .gitignore
  2. 29
    0
      Makefile
  3. 4
    2
      README.md
  4. 39
    0
      inc/game.hpp
  5. BIN
      res/helloworld.png
  6. 159
    0
      src/game.cpp
  7. 16
    0
      src/main.cpp

+ 4
- 0
.gitignore View File

@@ -1,3 +1,7 @@
bin/
lib/
obj/

# ---> C++
# Prerequisites
*.d

+ 29
- 0
Makefile View File

@@ -0,0 +1,29 @@
CXX = g++
CXXFLAGS = -std=c++14 -Wall -I./inc/ -I/usr/include/SDL2 -D_REENTRANT
SDL2LIBS = -pthread -lSDL2 -lSDL2_image -lSDL2_ttf -lSDL2_mixer -lSDL2_net

.PHONY: debug dirs push clean

debug: bin/main
bin/main

dirs:
mkdir -p bin/ lib/ obj/

bin/main: obj/game.o obj/main.o
@mkdir -p bin/
$(CXX) $(SDL2LIBS) $^ -o $@

obj/main.o: src/main.cpp inc/game.hpp
@mkdir -p obj/
$(CXX) $(CXXFLAGS) -c $< -o $@

obj/%.o: src/%.cpp inc/%.hpp
@mkdir -p obj/
$(CXX) $(CXXFLAGS) -c $< -o $@

push:
git add -A . && git commit -m "update `date +%F_%H:%M:%S`" && git push

clean:
$(RM) obj/* bin/*

+ 4
- 2
README.md View File

@@ -1,3 +1,5 @@
# making_game_with_sdl2
# Making game with SDL2

Making game with SDL2
## Install SDL2

`sudo pacman -S sdl2 sdl2_image sdl2_gfx sdl2_mixer sdl2_ttf sdl2_net`

+ 39
- 0
inc/game.hpp View File

@@ -0,0 +1,39 @@
#pragma once

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_net.h>
#include <SDL2/SDL_ttf.h>
#include <iostream>

enum ImageType {
ImageBMP,
ImageGIF,
ImageJPG,
ImagePNG,
};

class Game {
private:
SDL_Window *window = nullptr;
SDL_Surface *screen = nullptr;
SDL_Renderer *renderer = nullptr;
SDL_Surface *image_helloworld = nullptr;
SDL_Event event;
bool is_running = true;
bool window_fullscreen = true;
uint window_width = 0;
uint window_height = 0;
SDL_Surface *load_surface(const char *path, ImageType type);

public:
Game();
virtual ~Game();
bool running() { return is_running; }
bool init(const char *title, uint width, uint height, bool fullscreen);
void handle();
void update();
void render();
void close();
};

BIN
res/helloworld.png View File


+ 159
- 0
src/game.cpp View File

@@ -0,0 +1,159 @@
#include "game.hpp"

Game::Game() {}
Game::~Game() {}

bool Game::init(const char *title, uint width, uint height, bool fullscreen) {
window_width = width;
window_height = height;
window_fullscreen = fullscreen;
std::cout << "window width: " << width << std::endl;
std::cout << "window height: " << height << std::endl;
std::cout << "window fullscreen: " << window_fullscreen << std::endl;

// 初始化SDL
if (SDL_Init(SDL_INIT_EVERYTHING) == 0) {
std::cout << "SDL system: initialized" << std::endl;
} else {
std::cout << "SDL init failed! error: " << SDL_GetError() << std::endl;
return false;
}

// 初始化SDL_image
if (!(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG)) {
std::cout << "SDL_image init failed! error: " << SDL_GetError()
<< std::endl;
return false;
} else {
std::cout << "SDL image library: initialized" << std::endl;
}

// 创建window
window = SDL_CreateWindow(
title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height,
fullscreen ? SDL_WINDOW_FULLSCREEN : SDL_WINDOW_SHOWN);
if (window) {
std::cout << "SDL window: created" << std::endl;
} else {
std::cout << "window init failed! error: " << SDL_GetError() << std::endl;
return false;
}

// 创建screen
screen = SDL_GetWindowSurface(window);
if (screen) {
std::cout << "SDL screen surface: created" << std::endl;
} else {
std::cout << "screen init failed! error: " << SDL_GetError() << std::endl;
return false;
}

// 创建renderer
// renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
// if (renderer) {
// std::cout << "SDL renderer: created" << std::endl;
// SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
// } else {
// std::cout << "create renderer failed! error: " << SDL_GetError()
// << std::endl;
// return false;
// }

// 贴一张png试试
image_helloworld = load_surface("res/helloworld.png", ImagePNG);
if (image_helloworld) {
std::cout << "image_helloworld surface: loaded" << std::endl;
} else {
std::cout << "loading media failed! error: " << SDL_GetError() << std::endl;
return false;
}
SDL_Rect stretchRect;
stretchRect.x = 0;
stretchRect.y = 0;
stretchRect.w = window_width;
stretchRect.h = window_height;
SDL_BlitScaled(image_helloworld, NULL, screen, &stretchRect);
// SDL_BlitSurface(image_helloworld, NULL, screen, NULL);

return true;
}

void Game::handle() {
while (SDL_PollEvent(&event) != 0) {
switch (event.type) {
case SDL_QUIT:
is_running = false;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_ESCAPE:
is_running = false;
break;
case SDLK_w:
std::cout << "up" << std::endl;
break;
case SDLK_s:
std::cout << "down" << std::endl;
break;
case SDLK_a:
std::cout << "left" << std::endl;
break;
case SDLK_d:
std::cout << "right" << std::endl;
break;
default:
break;
}
break;
default:
break;
}
}
}

void Game::update() {}

void Game::render() { SDL_UpdateWindowSurface(window); }

void Game::close() {
if (image_helloworld) {
SDL_FreeSurface(image_helloworld);
image_helloworld = nullptr;
}
std::cout << "image_helloworld surface: released" << std::endl;

if (screen) {
SDL_FreeSurface(screen);
screen = nullptr;
}
std::cout << "SDL screen surface: released" << std::endl;

if (window) {
SDL_DestroyWindow(window);
window = nullptr;
}
std::cout << "SDL window: destroied" << std::endl;

SDL_Quit();
std::cout << "SDL Quit!" << std::endl;
}

SDL_Surface *Game::load_surface(const char *path, ImageType type) {
SDL_Surface *optimized_surface = nullptr;
SDL_Surface *loaded_surface = nullptr;
switch (type) {
case ImageBMP:
loaded_surface = SDL_LoadBMP(path);
break;
case ImagePNG:
default:
loaded_surface = IMG_Load(path);
break;
}
if (loaded_surface) {
optimized_surface = SDL_ConvertSurface(loaded_surface, screen->format, 0);
}
SDL_FreeSurface(loaded_surface);
loaded_surface = nullptr;
return optimized_surface;
}

+ 16
- 0
src/main.cpp View File

@@ -0,0 +1,16 @@
#include "game.hpp"

int main(int argc, char const *argv[]) {
Game *game = new Game();

if (game->init("main window", 640, 480, false)) {
while (game->running()) {
game->handle();
game->update();
game->render();
}
}

game->close();
return 0;
}

Loading…
Cancel
Save