01/10/2018, 15:00

Hỏi về tách file và SDL2

Chào mọi người, em mới học qua SDL c++.
E có vài vấn đề cần được giải đáp, mong mn giúp đỡ:
1: Tại sao kiểu dữ liệu của SDL thường là kiểu con trỏ vd: SDL_Texture*, SDL_Window*…
2: Em có một chương trình gồm 3 file đươc tách ra từ 1 file như sau ( Ở dưới). Khi chưa tách file thì hàm chạy bình thường và load image thành công, Khi em tách hàm ra thì chương trình không báo lỗi nhưng chỉ load được mỗi window chứ không có image. Bro nào giải thích và chỉ cách fix hộ em. em cảm ơn.
-------- Đây là file main.cpp --------

#include "common.h"
int main(int argc, char* argv[])
{
   init();
   bool quit=true;
   loadImage(background, "bg2.png");
   while(quit)
   {
       SDL_RenderPresent(renderer);
       if(SDL_PollEvent(&e))
       {
           if(e.type == SDL_QUIT)
           {
               quit=false;
           }
       }
   }
   cleanUp();
}

-------------Đây là file common.h------------

#ifndef COMMON_H
#define COMMON_H

#include <iostream>
#include <SDL.h>
#include <SDL_image.h>
using namespace std;

const int SCREEN_WIDTH =1200;
const int SCREEN_HEIGHT =600;

static SDL_Window *window;
static SDL_Renderer *renderer;
static SDL_Texture *background;
static SDL_Event e;

void init();
void cleanUp();
void loadImage(SDL_Texture* a, string path);

#endif // COMMON_H

------------Đây là file common.cpp -----------

#include "common.h"
void init()
{
    SDL_Init(SDL_INIT_EVERYTHING);
    IMG_Init(IMG_INIT_PNG);
    window = SDL_CreateWindow("game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    SDL_SetRenderDrawColor(renderer, 0xff, 0xff, 0xff, 0xff);
}
void cleanUp()
{
   SDL_DestroyWindow(window);
   SDL_DestroyRenderer(renderer);
SDL_DestroyTexture(background);
   SDL_Quit();
}
void loadImage(SDL_Texture* a, string path)
{
    SDL_Surface* surface;
    surface = IMG_Load(path.c_str());
    a = SDL_CreateTextureFromSurface(renderer, surface);
    SDL_FreeSurface(surface);
    SDL_RenderCopy(renderer, a, NULL, NULL);
}
Bài liên quan
0