Skip to content

Instantly share code, notes, and snippets.

@collinalexbell
Created July 13, 2015 08:55
Show Gist options
  • Save collinalexbell/b90124a28c4ad7bb77c1 to your computer and use it in GitHub Desktop.
Save collinalexbell/b90124a28c4ad7bb77c1 to your computer and use it in GitHub Desktop.
collinbell@Collins-MacBook-Air:~/taggartcybernetics/simulator$ make test_sim
g++ -c sim.cpp
In file included from sim.cpp:1:
./sim.h:41:37: warning: in-class initialization of non-static data member is a C++11 extension [-Wc++11-extensions]
bool bool_gui_test_finished = false;
^
./sim.h:42:30: warning: in-class initialization of non-static data member is a C++11 extension [-Wc++11-extensions]
bool bool_gui_result = false;
^
sim.cpp:69:5: error: no matching function for call to 'SDL_UpperBlit'
SDL_BlitSurface(message, NULL, full_message);
^~~~~~~~~~~~~~~
/usr/local/include/SDL/SDL_video.h:743:25: note: expanded from macro 'SDL_BlitSurface'
#define SDL_BlitSurface SDL_UpperBlit
^~~~~~~~~~~~~
/usr/local/include/SDL/SDL_video.h:748:29: note: candidate function not viable: requires 4 arguments, but 3 were provided
extern DECLSPEC int SDLCALL SDL_UpperBlit
^
sim.cpp:70:5: error: no matching function for call to 'SDL_UpperBlit'
SDL_BlitSurface(message2, NULL, full_message, yes_no_offset);
^~~~~~~~~~~~~~~
/usr/local/include/SDL/SDL_video.h:743:25: note: expanded from macro 'SDL_BlitSurface'
#define SDL_BlitSurface SDL_UpperBlit
^~~~~~~~~~~~~
/usr/local/include/SDL/SDL_video.h:748:29: note: candidate function not viable: no known conversion from 'SDL_Rect' to 'SDL_Rect *' for 4th argument;
take the address of the argument with &
extern DECLSPEC int SDLCALL SDL_UpperBlit
^
2 warnings and 2 errors generated.
make: *** [sim.o] Error 1
#include "sim.h"
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include <string>
#include <iostream>
Sim::Sim(int w, int h){
screen_width = w;
screen_height = h;
}
SDL_Surface* Sim::get_screen(){
return screen;
}
void Sim::init(){
SDL_Init(SDL_INIT_EVERYTHING);
TTF_Init();
screen = SDL_SetVideoMode(1080, 720, 32, SDL_SWSURFACE);
running = true;
#ifdef TEST_SDL_LOCK_OPTS
EM_ASM("SDL.defaults.copyOnLock = false; SDL.defaults.discardOnLock = true; SDL.defaults.opaqueFrontBuffer = false;");
#endif
if (SDL_MUSTLOCK(screen)) SDL_LockSurface(screen);
for (int i = 0; i < 256; i++) {
for (int j = 0; j < 256; j++) {
#ifdef TEST_SDL_LOCK_OPTS
// Alpha behaves like in the browser, so write proper opaque pixels.
int alpha = 255;
#else
// To emulate native behavior with blitting to screen, alpha component is ignored. Test that it is so by outputting
// data (and testing that it does get discarded)
int alpha = (i+j) % 255;
#endif
*((Uint32*)screen->pixels + i * 256 + j) = SDL_MapRGBA(screen->format, i, j, 255-i, alpha);
}
}
if (SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen);
SDL_Flip(screen);
}
void Sim::quit_sdl(){
SDL_Quit();
}
int Sim::start_gui_test_bool(char* test_text, bool desired_result){
bool_gui_test_finished = false;
int rv = 1;
TTF_Font *font = NULL;
SDL_Surface *message = NULL;
SDL_Surface *message2 = NULL;
SDL_Surface *full_message = NULL;
SDL_Rect yes_no_offset;
SDL_Rect full_message_offset;
yes_no_offset.y = 120;
yes_no_offset.x = 0;
full_message_offset.x = 0;
full_message_offset.y = 50;
SDL_Color textColor = { 255, 255, 255 };
font = TTF_OpenFont( "opensans.ttf", 14 );
message = TTF_RenderText_Solid( font, test_text, textColor );
message2 = TTF_RenderText_Solid( font, "Press Y/N", textColor );
SDL_BlitSurface(message, NULL, full_message);
SDL_BlitSurface(message2, NULL, full_message, yes_no_offset);
Drawable drawable = {full_message, full_message_offset};
things_to_draw.push_back(drawable);
return rv;
}
void Sim::draw(){
SDL_FillRect(screen, NULL, 0x000000);
int i;
for (i = 0; i < things_to_draw.size(); i++){
SDL_BlitSurface(things_to_draw[i].surf, NULL, screen, &things_to_draw[i].offset);
}
}
bool Sim::step(){
if( SDL_PollEvent( &event ) ) {
//If the user has Xed out the window
if( event.type == SDL_QUIT ) {
//Quit the program
return false;
}
if(event.type == SDL_KEYDOWN){
if(event.key.keysym.sym == SDLK_y){
bool_gui_test_finished = true;
bool_gui_result = true;
}
if( event.key.keysym.sym == SDLK_n){
bool_gui_test_finished = true;
bool_gui_result = false;
}
}
}
draw();
SDL_Flip(screen);
return true;
}
bool Sim::gui_test_finished(int test_id){
if(bool_gui_test_finished == true){
things_to_draw.erase(things_to_draw.begin() + test_id);
}
return bool_gui_test_finished;
}
bool Sim::get_gui_test_result(int test_id){
return bool_gui_result;
}
#ifndef SIM_H
#define SIM_H
#include <SDL/SDL.h>
#include <string>
#include <vector>
class Sim
{
public:
Sim(int w, int h);
void init();
void quit_sdl();
SDL_Surface* get_screen();
//Returns the id for the gui_test
int start_gui_test_bool(char* test_text, bool desired_result);
bool get_gui_test_result(int test_id);
bool gui_test_finished(int test_id);
bool step();
void draw();
void add_things_to_draw();
private:
struct Drawable{
SDL_Surface* surf;
SDL_Rect offset;
};
int screen_height;
int screen_width;
SDL_Surface *screen;
bool running;
std::vector<bool> gui_tests;
SDL_Event event;
bool bool_gui_test_finished = false;
bool bool_gui_result = false;
std::vector<Drawable> things_to_draw;
};
#endif
@collinalexbell
Copy link
Author

SDL_BlitSurface needs a SDL_Rect pointer and I was giving it an object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment