• Advertisement

SDL2 isn't drawing rectangles in Mac environment

Recommended Posts

I can't get SDL to make any rectangles on my Mac. The program I wrote worked completely fine on a Windows computer, but whenever I call the SDL_FillRect function on my Mac, nothing appears on the screen. I suspect this may be caused by a compilation issue. I'm using SDL through Frameworks. Inserted below is my terminal command and my code:

g++ example.cpp -I/Library/Frameworks/SDL2.framework/Headers -F/Library/Frameworks -framework SDL2
	#include <SDL2/SDL.h>
#include <iostream>
#include <stdio.h>
	const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int PLAYER_WIDTH = 20;
const int PLAYER_HEIGHT = 20;
SDL_Window* w = NULL;
SDL_Surface* s = NULL;
SDL_Renderer* r = NULL;
SDL_Rect BG = {0,0,SCREEN_WIDTH,SCREEN_HEIGHT};
	 
	bool init() {
	    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        printf("Couldn't initialize. error: %s\n", SDL_GetError() );
        return false;
    } else {
	        w = SDL_CreateWindow("test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
	
        if(w == NULL) {
            printf("Couldn't make window. error:%s\n", SDL_GetError() );
            return false;
        } else {
            s = SDL_GetWindowSurface(w);
            r = SDL_CreateRenderer(w,-1,SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
            SDL_SetRenderDrawColor(r,0,0,0,0);
            return true;
        }
	    }
	}
	void close() {
    SDL_FreeSurface(s);
    SDL_DestroyWindow(w);
    SDL_DestroyRenderer(r);
    SDL_Quit();
	}
	class Player {
	    private:
        int x;
        int y;
        int xvel;
        int yvel;
        int acceleration;
        SDL_Rect collider;
	
    public:
	        bool landed;
	        Player() {
            x = 0;
            y = SCREEN_HEIGHT - PLAYER_HEIGHT;
            xvel = 0;
            yvel = 0;
            acceleration = -1;
            collider.x = x;
            collider.y = y;
            collider.w = PLAYER_WIDTH;
            collider.h = PLAYER_HEIGHT;            
            landed = true;
        }
	        int getx() {
            return x;
        }
	        int gety() {
            return y;
        }
	        void setx(int num) {
            x = num;
        }
	        void sety(int num) {
            y = num;
        }
	        void setyvel(int num) {
            yvel += num;
        }
	        void setxvel(int num) {
            xvel += num;
        }
	        void move() {
            x += xvel;
	            if( (y + PLAYER_HEIGHT) > SCREEN_HEIGHT) {
                
                y = SCREEN_HEIGHT - PLAYER_HEIGHT;
                yvel = 0;
                landed = true;
	            } else {
                if(landed != true) { //so sorry for the bad code, this was the only idea i had to debug "bouncing" of the player
                    y += yvel;
                    yvel = yvel - acceleration;
                }
                
            }
	            
            collider.x = x;
            collider.y = y;
        }
	        void render() {
            SDL_SetRenderDrawColor(r,255,255,255,255);
            SDL_RenderFillRect(r,&collider);
            SDL_SetRenderDrawColor(r,0,0,0,255);
        }
};
	
int main(int argc, char *argv[]) {
	bool quit = false;
SDL_Event e; //event for event polling
Player p;
int count = 0;
	    if(init() == false) {
	        printf("Init failed: %s\n", SDL_GetError());
	    } else {
        while(quit == false ) {
            while( SDL_PollEvent(&e) != 0) {
	                if(e.type == SDL_QUIT) {
                    quit = true;
                }
	
                
                if(e.type == SDL_KEYDOWN) {
	                    switch(e.key.keysym.sym) {
	                        case SDLK_LEFT: if(e.key.repeat == 0) { p.setxvel(-10); }
                        break;
	                        case SDLK_RIGHT: if(e.key.repeat == 0) { p.setxvel(10); }
                        break;
	                        case SDLK_UP:
	                        if(count < 5) {
                            p.setyvel(-3);
                            ++count;
                            }
                            p.landed = false;
                        
                        break;
	                    }
	                }
	                
	                if(e.type == SDL_KEYUP && e.key.repeat == 0) {
	                    switch(e.key.keysym.sym) {
	                        case SDLK_LEFT: p.setxvel(10);
                        break;
	                        case SDLK_RIGHT: p.setxvel(-10);
                        break;
	                        case SDLK_UP: count = 0;
                        break;
	                    }
	                }
	                
	            }
	            SDL_RenderClear(r);
            SDL_RenderFillRect(r,&BG);
            p.move();
            p.render();
            
	            SDL_RenderPresent(r);
        }
    }
	    close();

Please help me to get it working, I have no idea what's going on. I have SDL 2.0.7 and my Mac is OS X 10.10.3.

Share this post


Link to post
Share on other sites
Advertisement

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By lonewolff
      Hi guys,
      I am having problems with trying to perform a basic 'shift left' on a char.
      char temp[1]; temp[0] = buffer[0] << 1; // buffer[0] is 0xff After this I have temp[0] writing to a file. Instead of being the expected 0x7F it is written as 0xF8.
      Any guidance on what I am doing wrong would be awesome.
      Thanks in advance
    • By sergio2k18
      Hi all
      this is my first post on this forum.
      First of all i want to say you that i've searched many posts on this forum about this specific argument, without success, so i write another one....
      Im a beginner.
      I want use GPU geometry clipmaps algorithm to visualize virtual inifinte terrains. 
      I already tried to use vertex texture fetch with a single sampler2D with success.
       
      Readed many papers about the argument and all speak about the fact that EVERY level of a geometry clipmap, has its own texture. What means this exactly? i have to 
      upload on graphic card a sampler2DArray?
      With a single sampler2D is conceptually simple. Creating a vbo and ibo on cpu (the vbo contains only the positions on X-Z plane, not the heights)
      and upload on GPU the texture containing the elevations. In vertex shader i sample, for every vertex, the relative height to te uv coordinate.
      But i can't imagine how can i reproduce various 2d footprint for every level of the clipmap. The only way i can imagine is follow:
      Upload the finer texture on GPU (entire heightmap). Create on CPU, and for each level of clipmap, the 2D footprints of entire clipmap.
      So in CPU i create all clipmap levels in terms of X-Z plane. In vertex shader sampling these values is simple using vertex texture fetch.
      So, how can i to sample a sampler2DArray in vertex shader, instead of upload a sampler2D of entire clipmap?
       
       
      Sorry for my VERY bad english, i hope i have been clear.
       
    • By mangine
      Hello. I am developing a civ 6 clone set in space and I have a few issues. I am using Lua for the logic and UI of the game and c++ directx 12 for the graphics. I need a way to send information between Lua and c++ occasionally and was wondering what is the best and most flexible (and hopefully fast) way to do this. Don't forget that I also need to send things from c++ back to Lua. I know of a lua extension called "LuaBridge" on github but it is a little old and I am worried that it will not work with directx 12. Has anybody done something similar and knows a good method of sending data back and forth? I am aware that Lua is used more and more in the industry and surely plenty of AAA game programmers know the answer to this. I want a good solution that will hopefully still be viable code in a couple of years...
    • By owenjr
      Hi there.
      I'm pretty new to this and I don't know if it has been asked before, but here I go.
      I'm developing a game using SFML and C++.
      I would like to use the "Tiled" tool to load maps into my game but I don't actually find any tutorial or guide on how to exaclty use it (I know that I have to read an XML file and stuff). I just step into diverse projects that make all a mess. 
      Anyone knows where can I find good information to make my map loader by myself?
      Thanks in advantage!!
    • By MHG OstryTM
      Hello guys,
      I've released my game for the first time. I'm very excited about it and I hope you'll enjoy the game - Beer Ranger. It's a retro-like puzzle-platfromer which makes you think a lot or die trying. You have a squad of skilled dwarfs with special powers and your goal is tasty beer. There is a lot of traps as well as many solutions how to endure them - it is up to your choice how to complete the level! 
      Link to the project: Project site
      Link to the Steam site with video: Beer Ranger
      Have fun and please write feedback if you feel up to.
      Some screens: 




  • Advertisement