Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SDL2/SDL.h>
- #include <SDL2/SDL_image.h>
- #include <SDL2/SDL_mixer.h>
- #include <iostream>
- #include <vector>
- #define win_width 600
- #define win_height 400
- #define fps 60
- using namespace std;
- void cap_framerate(Uint32 starting_tick){
- if((1000 / fps ) > SDL_GetTicks() - starting_tick){
- SDL_Delay(1000 / fps -(SDL_GetTicks() - starting_tick));
- }
- }
- class Sprite{
- protected:
- SDL_Surface *image;
- SDL_Rect rect;
- int origin_x, origin_y;
- public:
- Sprite(Uint32 color, int x, int y, int w =48, int h = 64){
- image = SDL_CreateRGBSurface(0,w,h,32,0,0,0,0);
- SDL_FillRect(image,NULL, color);
- rect = image->clip_rect;
- origin_x = rect.w / 2;
- origin_y = rect.h / 2;
- rect.x = x;
- rect.y = y;
- }
- void update(){
- }
- void draw(SDL_Surface *destination){
- SDL_BlitSurface(image, NULL, destination, &rect);
- }
- SDL_Surface* get_image() const{
- return image;
- }
- bool operator==(const Sprite &other) const {
- return(image == other.get_image());
- }
- };
- class SpriteGroup{
- private:
- vector <Sprite*> sprites;
- int sprites_size;
- public:
- SpriteGroup copy(){
- SpriteGroup new_group;
- for (int i = 0; i< sprites_size;i++){
- new_group.add(sprites[i]);
- }
- return new_group;
- }
- void add( Sprite *sprite ){
- sprites.push_back(sprite);
- sprites_size = sprites.size();
- }
- void remove(Sprite sprite_object){
- for(int i=0; i<sprites_size;i++){
- if( *sprites[i] == sprite_object){
- sprites.erase(sprites.begin() + i);
- }
- }
- sprites_size = sprites.size();
- }
- bool has(Sprite sprite_object){
- for(int i=0; i<sprites_size;i++){
- if( *sprites[i] == sprite_object){
- return true;
- }
- return false;
- }
- }
- void update(){
- if (!sprites.empty()){
- for(int i=0; i<sprites_size;i++){
- sprites[i]->update();
- }
- }
- }
- void draw(SDL_Surface *destination){
- if (!sprites.empty()){
- for(int i=0; i<sprites_size;i++){
- sprites[i]->draw(destination);
- }
- }
- }
- void empty(){
- sprites.clear();
- sprites_size = sprites.size();
- }
- int size(){
- return sprites_size;
- }
- vector <Sprite*> get_sprites(){
- return sprites;
- }
- };
- class Block : public Sprite{
- public:
- Block(Uint32 color, int x, int y, int w =48, int h = 64):
- Sprite(color,x,y,w,h){
- update_properties();
- }
- void update_properties(){
- origin_x=0;
- origin_y=0;
- set_position(rect.x,rect.y);
- }
- void set_position(int x,int y){
- rect.x = x - origin_x;
- rect.y = y - origin_y;
- }
- void set_image(const char filename[] = NULL){
- if(filename != NULL){
- SDL_Surface *loaded_image = NULL;
- loaded_image = IMG_Load(filename);
- if(loaded_image != NULL){
- image = loaded_image;
- int old_x = rect.x;
- int old_y = rect.y;
- rect = image->clip_rect;
- rect.x = old_x;
- rect.y = old_y;
- update_properties();
- }
- }
- }
- };
- int main(int argc, char* argv[])
- {
- SDL_Init( SDL_INIT_EVERYTHING);
- SDL_Window *win = NULL;
- win = SDL_CreateWindow("Sup", SDL_WINDOWPOS_UNDEFINED,
- SDL_WINDOWPOS_UNDEFINED, win_width,win_height,
- SDL_WINDOW_RESIZABLE);
- if (win == NULL){
- cout << "window error "<<endl<<SDL_GetError()<<endl;
- }
- SDL_Surface *screen = SDL_GetWindowSurface( win );
- Uint32 white = SDL_MapRGB(screen->format, 255,255,255);
- Uint32 red = SDL_MapRGB(screen->format, 255,0,0);
- Uint32 blue = SDL_MapRGB(screen->format, 0,0,255);
- SDL_FillRect(screen,NULL,white);
- //Sprite object(red, win_width/2, win_height/2);
- //Sprite another(blue, win_width/2 -100, win_height/2 +20);
- Block block(red,0,0);
- //block.set_image("sprite.bmp");
- SpriteGroup active_sprites;
- active_sprites.add(&block);
- active_sprites.draw(screen);
- SDL_UpdateWindowSurface(win);
- Uint32 starting_tick;
- SDL_Event event;
- bool running = true;
- while(running){
- starting_tick = SDL_GetTicks();
- while(SDL_PollEvent(&event)){
- if( event.type == SDL_QUIT){
- running = false;
- break;
- }
- }
- cap_framerate(starting_tick);
- }
- //SDL_Delay(5000);
- SDL_DestroyWindow(win);
- SDL_Quit();
- return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement