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();
}