Jump to content
  • Advertisement
CodeBro1

SDL_FillRect has no effect in Mac OS X environment

This topic is 390 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 tried to install SDL Unix-style using Homebrew. Inserted below is what I write into the terminal and my code.

 

g++ -o main test.cpp -L/usr/local/Cellar/sdl2/2.0.6/lib -lSDL2 -lSDL2main -I/usr/local/Cellar/sdl2/2.0.6/include/SDL2

 

#include <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) { 
					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();

}

 

Share this post


Link to post
Share on other sites
Advertisement
1 hour ago, CodeBro1 said:

whenever I call the SDL_FillRect function on my Mac, nothing appears on the screen.

What exactly happens? Do you just get one color on the whole screen? Does the window even open?

One thing I notice is that you're calling SDL_RenderClear and then filling in the background color. SDL_RenderClear actually fills in the screen so you shouldn't need the extra FillRect. See this

Share this post


Link to post
Share on other sites
17 hours ago, yyam said:

What exactly happens? Do you just get one color on the whole screen? Does the window even open?

One thing I notice is that you're calling SDL_RenderClear and then filling in the background color. SDL_RenderClear actually fills in the screen so you shouldn't need the extra FillRect. See this

Only one color appears on the screen: black. What is supposed to happen is that you're also supposed to the player's white rectangle. The window works fine, but the rendering doesn't.

Share this post


Link to post
Share on other sites

I tried your code in windows and can confirm it draws the white rectangle, shame I don't have a mac to debug this with D:

Another thing I notice is that your're linking to SDL2main and I believe that's only necessary on platforms that don't use a unix-style entry point. (See this) I Doubt it would do anything but have you tried not linking to that?

If that doesn't work then I'd suggest trying to get a minimal program going that just clears the screen to red or something just so you're 100% sure it's not a code issue.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!