C ++只使用h文件?

时间:2018-05-02 04:07:53

标签: c++ header

我可以通过使用头文件在c++进行编程吗?

我一直在尝试这样做,但在这里遇到这个文件的问题.. 它一直在说:

  

严重级代码描述项目文件行抑制状态   错误LNK2001未解析的外部符号" private:static int   发动机:: frameDelay"   (?frameDelay @ Engine @@ 0HA)引擎C:\ Users \ Shadowblitz16 \ Documents \ Visual   Studio 2017 \ Projects \ Zelda Classic 3.0 \ Zelda Classic 3.0 \ Test.obj 1

代码

#pragma once
#include "SDL.h"
#include "SDL_image.h"
#include <iostream>

class Engine
{
    private:
        static int frameTime;
        static int frameStart;
        static int frameDelay;
        static bool running;
        static SDL_Window   *window;
        static SDL_Renderer *renderer;

        static void HandleLoop()
        {
            while (running)              //Loop
            {
                //Start frame
                frameStart = SDL_GetTicks();
                HandleUpdate();
                HandleRender();

                //End frame 
                frameTime = SDL_GetTicks() - frameStart;
                if (frameDelay > frameTime) SDL_Delay(frameDelay - frameTime);
            }
        };
        static void HandleUpdate()
        {
            SDL_Event event;
            SDL_PollEvent(&event);
            switch (event.type)
            {
            case SDL_QUIT: running = false; break;
            default: break;
            }
            Update();
        };
        static void HandleRender()
        {
            SDL_RenderClear(renderer);
            Render();
            SDL_RenderPresent(renderer);
        }

    public:
        static void Run(const char* title, int x, int y, int width, int height, bool fullscreen)
        {
            frameTime  = 0;
            frameStart = 0;
            frameDelay = 0;

            window     = nullptr;
            renderer   = nullptr;

            running = false;

            if (SDL_Init(SDL_INIT_EVERYTHING) == 0)
            {
                std::cout << "SDL Initalized!..." << std::endl;
                window = SDL_CreateWindow(title, x, y, width, height, (fullscreen == true ? SDL_WINDOW_FULLSCREEN : 0));
                if (window) std::cout << "Window Created!" << std::endl;

                renderer = SDL_CreateRenderer(window, -1, 0);
                if (renderer) std::cout << "Renderer Created!" << std::endl;

                SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
                running = true;
            }

            frameStart = SDL_GetTicks(); //Get this so we can use it in the create event
            Create();                    //Run create event

            Clean();                     //Run clean event
            SDL_Quit();                  //Quit
        };

        static void Create() {};
        static void Update() {};
        static void Render() {};
        static void Clean()  {};

        static bool Running()              { return running;  };
        static SDL_Window*   GetWindow()   { return window;   };
        static SDL_Renderer* GetRenderer() { return renderer; };
};

我认为这种方式更容易阅读和调试,希望这是可能的

0 个答案:

没有答案