Jump to content
  • Advertisement

CodeBro1

Member
  • Content Count

    8
  • Joined

  • Last visited

Community Reputation

1 Neutral

About CodeBro1

  • Rank
    Newbie

Personal Information

  • Interests
    Art
    Programming
  1. 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.
  2. SDL isn't drawing any rectangles to the window in a Mac OS X environment, and I have no idea why. I've tried in both a Windows and an Ubuntu environment, and the rectangles are being drawn in those environments. I'm using SDL by using frameworks. This is what I write into the terminal: g++ example.cpp -I/Library/Frameworks/SDL2.framework/Headers -F/Library/Frameworks -framework SDL2 This is the actual code of the program: #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.
  3. 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.
  4. 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(); }
  5. I'm trying to find a way to check which frameworks, tools and game engines are popular, mainly because I'm trying to decide which programming language to learn, which would stem from would stem from which tool I'm going to use. Any advice?
  6. I made the diagram and I think I see what you're saying. The calculations of the camera's x and y coordinates keep the dot in the middle of the camera rectangle, which but the dot's "world coords", as you said, aren't necessarily in the middle of the camera coords, so the position the dot is drawn at is the dot's world coords subtracted by the extra distance the camera has moved from (0,0), which would be the camera's position. Does that sound like a reasonable explanation? Do you think I've understood?
  7. I've seen some SDL tutorials on implementing side-scrolling, and I don't understand how they do it. All of the tutorials I've seen do it like this:(taken from LazyFoo's code) camera.x = ( dot.getPosX() + Dot::DOT_WIDTH / 2 ) - SCREEN_WIDTH / 2; camera.y = ( dot.getPosY() + Dot::DOT_HEIGHT / 2 ) - SCREEN_HEIGHT / 2; This part, I at least understand. The camera's position is the position of the dot's origin minus the screen's width or height, because the camera should start moving should the dot's x or y coordinates exceed the middle of the screen. The part which I absolutely don't understand is this: void Dot::render( int camX, int camY ) { gDotTexture.render( mPosX - camX, mPosY - camY ); } In the above code, where the dot is drawn on the screen is the dot's x and y coordinates subtracted by the camera's x and y coordinates. I don't understand how or why one would come to the conclusion that the dot is to be rendered at the coordinates. I don't understand how it works. Can someone please explain?
  8. I am attempting to make a Pong game using SDL. I'm trying to render text on the screen, but segfaults prevent the code from running. I ran it in gdb, and found that the segfaults occur because SDL_QueryTexture doesn't give the ints passed into it any values. I put a statement to print SDL_GetError() after SDL_QueryTexture, and it says: "Failed loading SHCORE.DLL: The specified module could not be found." This wasn't happening after I made some adjustments to my code, so I believe the code is causing this problem. The relevant code is below: In case the rest of the code has a part in this, the entire .cpp file is attached. void loadText(string s) { SDL_Color color = {255,255,255}; SDL_Surface* textsurface = TTF_RenderText_Solid(font, s.c_str(), color); if(textsurface == NULL) { cout << "Could not rendertext onto surface" << endl; } texture = SDL_CreateTextureFromSurface(r,textsurface); if(texture == NULL) { cout << "Could not make texture" << SDL_GetError() << endl; } SDL_QueryTexture(texture, NULL, NULL, w, h); cout << SDL_GetError() << endl; SDL_FreeSurface(textsurface); textsurface = NULL; //cout << "Texture query error" << SDL_GetError() << endl; } void textRender(int x, int y) { destinationrect = {x,y,*w,*h}; if (SDL_RenderCopy(r,texture,NULL,&destinationrect) < 0) { cout << "Rendercopy error" << SDL_GetError() << endl; } } test.cpp OpenSans-Regular.ttf
  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!