球的移动0.5

· · 科技·工程

编译指令

-std=c++14 -Wall -mwindows -O3

-std=c++14 -Wall -mwindows -Og

连接器指令

-static-libgcc -lmsimg32

main

#include "winku.cpp"
#include "timer.hpp"
#include <algorithm>
#include <sstream>
#include <thread>
#include <fstream>
std::ofstream cerr("err.out"); using std::endl;

my_timer::timer_t<> timer;
//constexpr double EPS = 0.01, NINF = -1e5;
//constexpr int ARR_SIZE = 300;
class key_manager_t{
    static constexpr double EPS = 0.01, NINF = -1e5;
    static constexpr int ARR_SIZE = 300;
    double *_key_state;
    double _mouse_state;
    bool _mouse_down;
    int _mouse_x, _mouse_y;
    void _mouse_update(){
        if (_mouse_down){
            _mouse_state = timer.duration();
        }
        return ;
    }
    void _l_botton_down(winku::proc_arg_list_t arg){
        _mouse_down = true;
        return ; 
    }
    void _l_botton_up(winku::proc_arg_list_t arg){
        _mouse_down = false;
        return ;
    }
    void _mouse_move(winku::proc_arg_list_t arg){
        _mouse_x = LOWORD(arg.lparam);
        _mouse_y = HIWORD(arg.lparam);
        return ;
    }
    void _char(winku::proc_arg_list_t arg){
    //  cerr << arg.wparam << ' ' << (char)arg.wparam << endl;
        if (arg.wparam < ARR_SIZE) _key_state[arg.wparam] = timer.duration();
        return ;
    }
    void _check_key(char ch){
        if (GetKeyState(ch) >> 8) _key_state[(int)ch] = timer.duration();
        return ;
    }
    void _update_key(){
        for (char ch = 'A'; ch <= 'Z'; ch++){
            _check_key(ch);
        }
        for (char ch = '0'; ch <= '9'; ch++){
            _check_key(ch);
        }
        for (char ch = 'a'; ch <= 'z'; ch++){
            _check_key(ch);
        }
        return ;
    }
    void _thread_func(){
        while(1){
            _update_key();
        }
        return ;
    }
public:
    key_manager_t():_key_state(new double[ARR_SIZE])
        , _mouse_state(NINF), _mouse_down(false), _mouse_x(0), _mouse_y(0) {
        std::fill(_key_state, _key_state + ARR_SIZE, NINF);
        (new std::thread(_thread_func, this))->detach();
    }
    void update(winku::proc_arg_list_t arg){
        _mouse_update();
        switch(arg.message){
    case WM_LBUTTONDOWN:
            _l_botton_down(arg);
            break;
    case WM_LBUTTONUP:
            _l_botton_up(arg);
            break;
    case WM_MOUSEMOVE:
            _mouse_move(arg);
            break;
    case WM_CHAR:

            break;
    default:
            break;
        }
        return ;
    }
    bool key_down(char ch){
    //  cerr << ch << ' ' << _key_state[ch] << ' ' << timer.duration() << endl;
        return _key_state[(int)ch] + EPS >= timer.duration(); 
    } 
    bool mouse_down(){
        return _mouse_state + EPS >= timer.duration();
    }
    int mouse_x(){
        return _mouse_x;
    }
    int mouse_y(){
        return _mouse_y;
    }
};
key_manager_t key_manager;
struct chara_t{

    int min_x, max_x, min_y, max_y;
    int last_x, last_y, r;
    char up, down, left, right;
    int speed; double delta_time, next_time;
    COLORREF color;
    int layer_id;

    chara_t():min_x(1), max_x(winku::win_info.win_width), min_y(1), max_y(winku::win_info.win_height),
        last_x(100), last_y(100), r(20),
        up('W'), down('S'), left('A'), right('D'),
        speed(2), delta_time(0.005), next_time(0.0),
        color(RGB(255, 0, 0)), layer_id(1){}

    void shrink_pos(){
        last_x = std::max(last_x, min_x);
        last_x = std::min(last_x, max_x);
        last_y = std::max(last_y, min_y);
        last_y = std::min(last_y, max_y);
        return ;
    }

    void move(){
        using namespace winku;
        if (timer.duration() < next_time) return ;
    //  std::stringstream ss;
    //  ss << "it is " << arg.wparam << "  to (char: " << (char)arg.wparam;
    //  if(arg.wparam != 0) MessageBox(nullptr, ss.str().c_str(), "anxia", 0);
        if(key_manager.key_down(up) && key_manager.key_down(left)){
            pic_layer_map[layer_id].dot(last_x, last_y, r, true);
            last_y -= speed;
            last_x -= speed;

        }else if(key_manager.key_down(up) && key_manager.key_down(right)){
            pic_layer_map[layer_id].dot(last_x, last_y, r, true);
            last_y -= speed;
            last_x += speed;

        }else if(key_manager.key_down(down) && key_manager.key_down(left)){
            pic_layer_map[layer_id].dot(last_x, last_y, r, true);
            last_y += speed;
            last_x -= speed;

        }else if(key_manager.key_down(down) && key_manager.key_down(right)){
            pic_layer_map[layer_id].dot(last_x, last_y, r, true);
            last_y += speed;
            last_x += speed;

        }else if(key_manager.key_down(up)){
        //  cerr<<layer_id<<endl;
            pic_layer_map[layer_id].dot(last_x, last_y, r, true);
            last_y -= speed;

        }else if(key_manager.key_down(down)){
            pic_layer_map[layer_id].dot(last_x, last_y, r, true);
            last_y += speed;

        }else if(key_manager.key_down(left)){
            pic_layer_map[layer_id].dot(last_x, last_y, r, true);
            last_x -= speed;

        }else if(key_manager.key_down(right)){
            pic_layer_map[layer_id].dot(last_x, last_y, r, true);
            last_x += speed;

        }else{
            return ;
        }
        next_time = timer.duration() + delta_time;
        shrink_pos();
        pic_layer_map[layer_id].tmp_color = color;
        pic_layer_map[layer_id].dot(last_x, last_y, r);
        need_update = true;
        return ;
    }

    void thread_func(){
        while(1){
            move();
        }
        return ;
    }

    void show(){
        using namespace winku;
        pic_layer_map[layer_id].tmp_color = color;
        pic_layer_map[layer_id].dot(last_x, last_y, r);
        return ;
    }

    void main(){
        show();
        (new std::thread(thread_func, this))->detach();
        return ;
    }

/*
    void move(winku::proc_arg_list_t arg){
        using namespace winku;
        if (timer.duration() < next_time) return ;
        next_time = timer.duration() + delta_time;
    //  std::stringstream ss;
    //  ss << "it is " << arg.wparam << "  to (char: " << (char)arg.wparam;
    //  if(arg.wparam != 0) MessageBox(nullptr, ss.str().c_str(), "anxia", 0);
        if(arg.wparam==up){
            pic_layer_map[layer_id].dot(last_x, last_y, r, true);
            last_y -= speed;
        }else if(arg.wparam==down){
            pic_layer_map[layer_id].dot(last_x, last_y, r, true);
            last_y += speed;
        }else if(arg.wparam==left){
            pic_layer_map[layer_id].dot(last_x, last_y, r, true);
            last_x -= speed;
        }else if(arg.wparam==right){
            pic_layer_map[layer_id].dot(last_x, last_y, r, true);
            last_x += speed;
        }else{
            return ;
        }
        shrink_pos();
        pic_layer_map[layer_id].tmp_color = color;
        pic_layer_map[layer_id].dot(last_x, last_y, r);
        need_update = true;
        return ;
    }*/ 
};
chara_t l_hand_move, r_hand_move;
namespace winku{
    void set_win(winmain_arg_list_t){
        win_info.caption = "movement";
        return ;
    }

    void main(proc_arg_list_t){
        pic_layer_map[0].tmp_color = RGB(200, 200, 0);

        pic_layer_map[0].bfs(1, 1);

        pic_layer_map[1];

        l_hand_move = chara_t();
        l_hand_move.main();

        pic_layer_map[2];

        r_hand_move = chara_t();
        r_hand_move.up = 'I';
        r_hand_move.down = 'K';
        r_hand_move.left = 'J';
        r_hand_move.right = 'L';
        r_hand_move.layer_id = 2;
        r_hand_move.color = RGB(0, 0, 255);
        r_hand_move.main();
        return ;
    }

    void win_proc(proc_arg_list_t arg){
        key_manager.update(arg);
        return ;
    }


}

kinku0.6(my_map)

#ifndef __MIAO_MIAO_WINMAN__
#define __MIAO_MIAO_WINMAN__ __miao_miao__
#include <cassert>
#include <windows.h>
#include <string>
#include <queue>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <queue>

namespace my_map{
    template<typename Tp>
    class map_t{
        static constexpr int ARR_SIZE = 10;
        Tp **_arr, **_begin, **_end;
        class iterator{
            map_t* _host;
            Tp** _iter;
    public:
            iterator():_host(nullptr), _iter(nullptr){}
            iterator(map_t* __host, Tp** __iter)
                :_host(__host), _iter(__iter){}
            iterator& operator++(){
                while (_iter != _host->_end){
                    ++_iter;
                    if (*_iter != nullptr) break;
                }
                return *this;
            }
            bool operator!=(const iterator& other){
                return _host != other._host || _iter != other._iter;
            }
            Tp& operator*(){
                return **_iter;
            }
        };
public:
        map_t(){
            _begin = new Tp*[ARR_SIZE << 1 | 1];
            std::fill(_begin, _begin + (ARR_SIZE << 1 | 1), nullptr);
            _arr = _begin + ARR_SIZE;
            _end = _begin + (ARR_SIZE << 1 | 1);
            return ;
        }
        bool count(int it){
            return _arr[it] != nullptr;
        }
        void emplace(int it){
            _arr[it] = new Tp;
            return ;
        }
        iterator begin(){
            iterator ret(this, _begin);
            ++ret;
            return ret;
        }
        iterator end(){
            return iterator(this, _end);
        }
        Tp& operator[](int it){
            if (!count(it)){
                emplace(it);
            }
            return *_arr[it];
        }
    };
}

namespace winku{

    struct pos_t{
        int x, y;
        pos_t():x(0),y(0){}
        pos_t(int _x, int _y):x(_x), y(_y){}
    };

    namespace math{
        long long pow_2(long long d){
            return d * d;
        }
    }

    namespace mem{
        template<typename Tp>
        void alloc(Tp*& arr, int si){
            arr = new Tp[si];
            return ;
        }

        template<typename Tp, typename... Arg>
        void alloc(Tp*& arr, int si, Arg... ls){
            arr = new Tp[si];
            for (int i = 0; i < si; i++){
                alloc(arr[i], ls...);
            }
        }

        template<typename Tp>
        void free(Tp* arr){
            delete[] arr;
            return ;
        }

        template<typename Tp, typename... Arg>
        void free(Tp* arr, int si, Arg... ls){
            for (int i = 0; i < si; i++) free(arr[i], ls...);
            delete[] arr;
            return ;
        }
    }

    std::ofstream ferr("err.out"); using std::endl;

    constexpr int SIZE_OF_ST = 1024;

    struct win_info_t{
        std::string class_name;
        std::string caption;
        int win_pos_x;
        int win_pos_y;
        int win_width;
        int win_height;
        int background;
    //  std::string cursor;
    //  std::string icon;
        std::string menu_name;
        int style;
        win_info_t():
            class_name("DefautClassName"),
            caption("DefautCaption"),
            win_pos_x(100),
            win_pos_y(100),
            win_width(1280),
            win_height(960),
            background(0),
    //      cursor(IDC_ARROW),
    //      icon(IDI_APPLICATION),
            menu_name("DefautMenuName"){}
    } win_info;

    struct winmain_arg_list_t{
        HINSTANCE hInst;
        HINSTANCE hPrev;
        std::string lpCmdLine;
        int nShowCmd;
        winmain_arg_list_t(){}
        winmain_arg_list_t(HINSTANCE _hInst, 
                    HINSTANCE _hPrev, 
                    std::string _lpCmdLine, 
                    int _nShowCmd):
                    hInst(_hInst),
                    hPrev(_hPrev),
                    lpCmdLine(_lpCmdLine),
                    nShowCmd(_nShowCmd){}
    };

    struct proc_arg_list_t{
        HWND hwnd;
        unsigned message;
        unsigned long long wparam;
        long long lparam;
        proc_arg_list_t(){}
        proc_arg_list_t(HWND _hwnd,
                unsigned _message,
                unsigned long long _wparam,
                long long _lparam):
                hwnd(_hwnd),
                message(_message),
                wparam(_wparam),
                lparam(_lparam){}
    };

    struct dot_t{
        bool alive;
        COLORREF color;
        dot_t():alive(false), color(RGB(0, 0, 0)){}
        void clear(){
            alive = false;
            color = RGB(0, 0, 0);
            return ;
        }
        void set(COLORREF _color){
            color = _color;
            alive = true;
        }
        bool operator==(dot_t& _b){
            return alive == _b.alive && color == _b.color;
        }
    };

    struct bitmap_t{
        static constexpr unsigned short red = 2, green = 1, blue = 0;
        unsigned char* arr;
        unsigned short width, height, byte_len;
        bitmap_t():arr(nullptr), width(0), height(0), byte_len(0){}
        void clear(){
            std::fill(arr, arr + byte_len * height, 0);
            return ;
        }
        void res(){
            width = win_info.win_width;
            height = win_info.win_height;
            byte_len = width * 3 & (~3);
            arr = new unsigned char[byte_len * height + 1000];
            clear();
            return ;
        }
        unsigned char& at(unsigned short x, unsigned short y, unsigned short col){
            return arr[x * 3 + y * byte_len + col];
        }
        void set(int x, int y, COLORREF color){
            at(x, y, red) = GetRValue(color);
            at(x, y, green) = GetGValue(color);
            at(x, y, blue) = GetBValue(color);
            return ;
        }
    };

    namespace _bfs_help{
        static constexpr int rdx[4] = {1, -1, 0, 0};
        static constexpr int rdy[4] = {0,  0, 1,-1};
    }

    struct pic_layer_t{
        dot_t** arr;
        int maxx, maxy;
        COLORREF tmp_color;
        void res(){
            mem::alloc(arr, win_info.win_width, win_info.win_height);
            maxx = win_info.win_width; maxy = win_info.win_height;
            return ;
        }

        pic_layer_t(int _hahaha):arr(nullptr), maxx(-1), maxy(-1), tmp_color(RGB(225, 0, 0)){}

        pic_layer_t():arr(nullptr), maxx(-1), maxy(-1), tmp_color(RGB(225, 0, 0)){
            res();
        }
        ~pic_layer_t(){
            if(arr != nullptr){
                mem::free(arr, win_info.win_width); 
            }
        }
        bool is_ok(int x, int y){
            return 0 <= x && x < maxx && 0 <= y && y < maxy;
        }
        bool set(int x, int y, bool is_clear = false){
            if (!is_ok(x, y)) return false;
            if (is_clear){
                arr[x][y].clear();
            }else{
                arr[x][y].set(tmp_color);
            }
            return true;
        }
        void clear(){
            for (int x = 0; x < maxx; x++){
                std::fill(arr[x], arr[x] + maxy, dot_t());
            }
            return ;
        }

        void bfs(int x, int y, bool is_clear = false){
            using namespace _bfs_help;
            static bool** vis = nullptr;
            if (vis == nullptr){
                mem::alloc(vis, maxx + 1, maxy + 1);
            }
            for (int i = 0; i < maxx; i++){
                std::fill(vis[i], vis[i] + maxy, false);
            }
            std::queue<pos_t> wt; wt.emplace(x, y);
            set(x, y, is_clear); vis[x][y] = true;
            while(!wt.empty()){
                pos_t tmp = wt.front(); wt.pop();
                for (int i = 0; i < 4; i++){
                    pos_t n(tmp.x + rdx[i], tmp.y + rdy[i]);
                    if (!is_ok(n.x, n.y) || vis[n.x][n.y]) continue;
                    vis[n.x][n.y] = true;
                    wt.push(n);
                    set(n.x, n.y, is_clear);
                }
            }
            return ;
        }
        void dot(int x, int y, int r = 2, bool is_clear = false){
            using namespace math;
            for (int dx = 0; dx <= r; dx++){
                for (int dy = 0; dy <= r; dy++){
                    if (pow_2(dx) + pow_2(dy) <= pow_2(r)){
                        set(x + dx, y + dy, is_clear);
                        set(x - dx, y + dy, is_clear);
                        set(x - dx, y - dy, is_clear);
                        set(x + dx, y - dy, is_clear);
                    }
                }
            }
            return ;
        }
        void line(int x1, int y1, int x2, int y2, int r = 2, bool is_clear = false){
            if (x2 == x1){
                if (y1 > y2) std::swap(y1, y2);
                for (int y = y1; y <= y2; y++){
                    dot(x1, y, r, is_clear);
                }
            }else if(abs(y1 - y2) < abs(x1 - x2)){
                if (x1 > x2){
                    std::swap(x1, x2);
                    std::swap(y1, y2);
                }
                for (int x = x1; x <= x2; x++){
                    dot(x, 1ll * (y2 - y1) * (x - x1) / (x2 - x1) + y1, r, is_clear);
                }
            }else{
                if (y1 > y2){
                    std::swap(x1, x2);
                    std::swap(y1, y2);
                }
                for (int y = y1; y <= y2; y++){
                    dot(1ll * (x2 - x1) * (y - y1) / (y2 - y1) + x1, y, r, is_clear);
                }
            }
        }
        void draw_to(pic_layer_t& other){
            for (int x = 0; x < maxx; x++){
                for (int y = 0; y < maxy; y++){
                    if (!arr[x][y].alive) continue;
                    other.tmp_color = arr[x][y].color;
                    other.set(x, y);
                }
            }
            return ;
        }
        void draw_to(bitmap_t& bit_map){
            for (int x = 0; x < maxx; x++){
                for (int y = 0; y < maxy; y++){
                    if (!arr[x][y].alive) continue;
                    bit_map.set(x, y, arr[x][y].color);
                }
            }
            return ;
        }
    };

    bitmap_t main_bit_map;
    BITMAPINFO main_bit_map_info;

    void set_win(winmain_arg_list_t);

    void win_proc(proc_arg_list_t);

    void main(proc_arg_list_t);

    namespace _hdc_space{
        HDC hdc, hmap_dc;
        HBITMAP hmap;
    }

    void _begin_work(proc_arg_list_t arg){
        using namespace _hdc_space;
        main_bit_map.res();
        main_bit_map_info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
        main_bit_map_info.bmiHeader.biWidth = main_bit_map.width;
        main_bit_map_info.bmiHeader.biHeight = -main_bit_map.height;
        main_bit_map_info.bmiHeader.biPlanes = 1;
        main_bit_map_info.bmiHeader.biBitCount = 24;
        main_bit_map_info.bmiHeader.biCompression = BI_RGB;
        main_bit_map_info.bmiHeader.biSizeImage = 0;
        main_bit_map_info.bmiHeader.biClrUsed = 0;
        main_bit_map_info.bmiHeader.biClrImportant = 0;//填写位图头

        hdc = GetDC(arg.hwnd);

        hmap_dc = CreateCompatibleDC(nullptr);

        hmap = CreateCompatibleBitmap(hdc, win_info.win_width, win_info.win_height);

        SelectObject(hmap_dc, hmap);
        return ;
    }

//  std::map<int, pic_layer_t> pic_layer_map;
    my_map::map_t<pic_layer_t> pic_layer_map;
    bool need_update = false;

    void _update_window(proc_arg_list_t list){
        using namespace _hdc_space;
        main_bit_map.clear();

        for (auto& tmp_map : pic_layer_map){
            tmp_map.draw_to(main_bit_map);
        }

        SetDIBits(hmap_dc, hmap, 0, win_info.win_height, main_bit_map.arr,
            &main_bit_map_info, DIB_RGB_COLORS);

        BitBlt(hdc, 0, 0,
            win_info.win_width, win_info.win_height,
            hmap_dc, 0, 0, SRCCOPY);
        return ;
    }

    void _end_work(proc_arg_list_t list){
        using namespace _hdc_space;
        ReleaseDC(list.hwnd, hdc);
        exit(0);
        return ;
    }

    long long _window_proc(HWND hwnd, unsigned message, unsigned long long wparam, long long lparam){
        static bool is_begin = true;
        auto list = proc_arg_list_t(hwnd, message, wparam, lparam);
        need_update = false;
        if (is_begin){
            _begin_work(list);
            main(list);
            is_begin = false;
        }
        switch(message){
    case WM_QUIT:
    case WM_CLOSE:
    case WM_DESTROY:
            _end_work(list); 
            break;
    default:
            break;
        }
        win_proc(list);
        _update_window(list);
        return DefWindowProc(hwnd, message, wparam, lparam);
    }

}
int __stdcall WinMain(HINSTANCE hInst, HINSTANCE hPrev, char* lpCmdLine, int nShowCmd){

    using namespace winku;

    set_win(winmain_arg_list_t(hInst, hPrev, lpCmdLine, nShowCmd));

    WNDCLASSA wc;

    memset(&wc, 0, sizeof(wc));
    wc.hbrBackground = (HBRUSH)GetStockObject(win_info.background);//-mwindows
    wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
    wc.hIcon = LoadIcon(nullptr, IDI_APPLICATION);
    wc.lpszClassName = win_info.class_name.c_str();
    wc.hInstance = hInst;
    wc.lpfnWndProc = _window_proc;
    wc.lpszMenuName = win_info.menu_name.c_str();
    wc.style = win_info.style;
    RegisterClassA(&wc);

    HWND hwnd = 
        CreateWindowA(win_info.class_name.c_str(),
                win_info.caption.c_str(),
                WS_VISIBLE | WS_OVERLAPPEDWINDOW,
                win_info.win_pos_x, win_info.win_pos_y,
                win_info.win_width,  win_info.win_height,
                nullptr, nullptr, hInst, nullptr);

    ShowWindow(hwnd, SW_SHOWNORMAL);
    UpdateWindow(hwnd);

    MSG message;

    while(true){
        if (GetMessage(&message, nullptr, 0, 0)){
            TranslateMessage(&message);
            DispatchMessageA(&message);
        }
    }

    return 0;
}
#endif

timer

#include<chrono>
namespace my_timer{
    template <typename _Clock=std::chrono::steady_clock,typename _Dur=std::chrono::duration<double,std::ratio<1,1> > >
    class timer_t{//用于算法的计时 
        private:
            int cnt;
            std::chrono::time_point<_Clock,_Dur> start_time;
        public:
            timer_t(){
                cnt=0;
                start_time=_Clock::now();
            }
            void start(){//开始新的计时 
                cnt++;
                start_time= _Clock::now();
                return ;
            }
            double duration(){//获取从开始计时到现在的时间段 
                std::chrono::time_point<_Clock,_Dur> end_time= _Clock::now();
                return (end_time-start_time).count();
            }
            int times(){//获取开始计时的次数 
                return cnt;
            }
    };  
}