static Color colorFromName(const char *name) { for(size_t i=0; i < sizeof(colorNames)/sizeof(colorNames[0]); i++) { const ColorName &c=colorNames[i]; if (strcmp(c.name, name)==0) { return Color(c.r, c.g, c.b, 0xFF); } } return Color(255,255,255,255); }
//returns the possible move location in ChessBoard::BoardSlot position std::vector<sf::Vector2i> King::getPossibleMoveLocation(ChessBoard& gameBoard)const{ std::vector<sf::Vector2i> possibleLocations; using sf::Vector2i; Vector2i spritePos( int(m_sprite.GetPosition().x),int(m_sprite.GetPosition().y)); Vector2i slotPos = gameBoard.convertToBoardIndex(spritePos.x,spritePos.y); Vector2i tmp(slotPos.x - 1, slotPos.y - 1); //check possible moves in previous row if any if(tmp.x >= 0) { for(int i = 0; i < 3; ++i, ++tmp.y ) { if(tmp.y >= 0 && tmp.y < ChessBoard::BOARD_WIDTH){ //if not occupied highlight it if(_canHighlightSlot(gameBoard, tmp) != NOT_HIGHLIGHTED){ possibleLocations.push_back(tmp); } } } } //check possible moves in forward row if any tmp = Vector2i(slotPos.x + 1, slotPos.y - 1); if(tmp.x < ChessBoard::BOARD_HEIGHT) { for(int i = 0; i < 3; ++i, ++tmp.y ) { if(tmp.y >= 0 && tmp.y < ChessBoard::BOARD_WIDTH){ if(_canHighlightSlot(gameBoard, tmp) != NOT_HIGHLIGHTED){ possibleLocations.push_back(tmp); } } } } //check left slot tmp = Vector2i(slotPos.x, slotPos.y - 1); if(tmp.y >= 0) { if(_canHighlightSlot(gameBoard, tmp) != NOT_HIGHLIGHTED){ possibleLocations.push_back(tmp); } } //check right slot tmp = Vector2i(slotPos.x, slotPos.y + 1); if(tmp.y < ChessBoard::BOARD_WIDTH) { if(_canHighlightSlot(gameBoard, tmp) != NOT_HIGHLIGHTED){ possibleLocations.push_back(tmp); } } return possibleLocations; }
int main() { RenderWindow window(VideoMode(1280, 720), "Link Testing Grounds"); window.setVerticalSyncEnabled(true); RectangleShape hud1(Vector2f(90, 75)); hud1.setFillColor(Color::Transparent); hud1.setOutlineThickness(10); hud1.setOutlineColor(Color::Blue); hud1.setOrigin(hud1.getLocalBounds().width, 0); hud1.setPosition(window.getSize().x / 2, 0); RectangleShape hud2(Vector2f(90, 75)); hud2.setFillColor(Color::Transparent); hud2.setOutlineThickness(10); hud2.setOutlineColor(Color::Blue); hud2.setOrigin(0, 0); hud2.setPosition(window.getSize().x / 2, 0); Text item1; Font font; if (!font.loadFromFile("sansation.ttf")) { cout << "Could not load font." << endl; window.close(); } item1.setFont(font); item1.setString("A"); item1.setCharacterSize(32); item1.setColor(Color::Blue); item1.setPosition((window.getSize().x / 2) - 105, 0); Text item2; item2.setFont(font); item2.setString("B"); item2.setCharacterSize(32); item2.setColor(Color::Blue); item2.setPosition((window.getSize().x / 2) + 10, 0); // Animations //Sprite sl[] = { sl0, sl1, sl2, sl3, sl4 }; //Sprite sr[] = { sr0, sr1, sr2, sr3, sr4 }; //Sprite su[] = { su0, su1, su2, su3, su4 }; //Sprite sd[] = { sd0, sd1, sd2, sd3, sd4, sd5 }; //for (unsigned int i = 0; i < 5; i++) { // sl[i].setTexture(char_link); // sl[i].setOrigin(sl[i].getLocalBounds().width, min(sl[i].getLocalBounds().height, wl[0].getLocalBounds().height)); // sr[i].setTexture(char_link); // sr[i].setOrigin(min(sr[i].getLocalBounds().width, wr[0].getLocalBounds().width), min(sr[i].getLocalBounds().height, wr[0].getLocalBounds().height)); // su[i].setTexture(char_link); // su[i].setOrigin(max(su[i].getLocalBounds().width, wu[0].getLocalBounds().width), su[i].getLocalBounds().height); //} //for (unsigned int i = 0; i < 6; i++) { // sd[i].setTexture(char_link); // sd[i].setOrigin(min(sd[i].getLocalBounds().width, wd[0].getLocalBounds().width), wd[0].getLocalBounds().height); //} /* DIRECTION 0 : Not Moving 1 : WEST 2 : EAST 3 : NORTH 4 : SOUTH */ //unsigned int facing = 1; /*bool swingSword = false; unsigned int swingSpeed = 0; unsigned int swingCount = 0;*/ load(); bool pause = false; while (window.isOpen()) { Event event; bool isEvent = false; while (window.pollEvent(event)) { switch (event.type) { case Event::Closed: isEvent = true; window.close(); break; case Event::KeyPressed: if (event.key.code == Keyboard::Escape) { isEvent = true; window.close(); } /*if (event.key.code == Keyboard::S) { isEvent = true; window.setKeyRepeatEnabled(false); swingSword = true; }*/ break; case Event::LostFocus: pause = true; cout << endl << "-----------------------PAUSED-----------------------" << endl; break; case Event::GainedFocus: pause = false; cout << endl << "----------------------UNPAUSED----------------------" << endl; break; default: break; } } // END WHILE - EVENT POLL bool madeMove = false; if (!pause) { /* if (!Keyboard::isKeyPressed(Keyboard::Left) && direction == 1) { direction = 0; } if (!Keyboard::isKeyPressed(Keyboard::Right) && direction == 2) { direction = 0; } if (!Keyboard::isKeyPressed(Keyboard::Up) && direction == 3) { direction = 0; } if (!Keyboard::isKeyPressed(Keyboard::Down) && direction == 4) { direction = 0; }*/ if (Keyboard::isKeyPressed(Keyboard::Left)) { } if (Keyboard::isKeyPressed(Keyboard::Right)) { } if (Keyboard::isKeyPressed(Keyboard::Up)) { } if (Keyboard::isKeyPressed(Keyboard::Down)) { } if (!madeMove) { } /*if (swingSword) { swingSpeed++; if (facing == 1) { if (swingCount == 5 || swingCount == 6) { swingSword = false; swingCount = 0; } state = sl[swingCount]; if (swingSpeed == SWORD_SPEED) { cout << "Left swing count: " << swingCount << endl; swingCount++; swingSpeed = 0; } } else if (facing == 2) { if (swingCount == 5 || swingCount == 6) { swingSword = false; swingCount = 0; } state = sr[swingCount]; if (swingSpeed == SWORD_SPEED) { cout << "Right swing count: " << swingCount << endl; swingCount++; swingSpeed = 0; } } else if (facing == 3) { if (swingCount == 5 || swingCount == 6) { swingSword = false; swingCount = 0; } state = su[swingCount]; if (swingSpeed == SWORD_SPEED) { cout << "Up swing count: " << swingCount << endl; swingCount++; swingSpeed = 0; } } else { if (swingCount == 6) { swingSword = false; swingCount = 0; } state = sd[swingCount]; if (swingSpeed == SWORD_SPEED) { cout << "Down swing count: " << swingCount << endl; swingCount++; swingSpeed = 0; } } */ } state.move(position_x, position_y); } // END IF - PAUSED STATE window.clear(); window.draw(state); window.draw(hud1); window.draw(hud2); window.draw(item1); window.draw(item2); window.display(); if (previousPosition_x != state.getPosition().x || previousPosition_y != state.getPosition().y) { cout << state.getPosition().x << ", " << state.getPosition().y << "\t"; if (facing == 1) { cout << "left" << endl; } else if (facing == 2) { cout << "right" << endl; } else if (facing == 3) { cout << "up" << endl; } else if (facing == 4) { cout << "down" << endl; } previousPosition_x = state.getPosition().x; previousPosition_y = state.getPosition().y; } } //END WHILE - WINDOW OPEN
//returns the possible move location in ChessBoard::BoardSlot position std::vector<sf::Vector2i> Knight::getPossibleMoveLocation(ChessBoard& gameBoard)const{ std::vector<sf::Vector2i> possibleLocations; using sf::Vector2i; Vector2i spritePos( int(m_sprite.GetPosition().x),int(m_sprite.GetPosition().y)); Vector2i slotPos = gameBoard.convertToBoardIndex(spritePos.x,spritePos.y); Vector2i tmp; //front mid left tmp = Vector2i(slotPos.x + 1, slotPos.y - 2); if(tmp.x < ChessBoard::BOARD_HEIGHT && tmp.y >= 0){ if(_canHighlightSlot(gameBoard,tmp) != NOT_HIGHLIGHTED){ possibleLocations.push_back(tmp); } } //front mid right tmp = Vector2i(slotPos.x + 1, slotPos.y + 2); if(tmp.x < ChessBoard::BOARD_HEIGHT && tmp.y < ChessBoard::BOARD_WIDTH){ if(_canHighlightSlot(gameBoard,tmp) != NOT_HIGHLIGHTED){ possibleLocations.push_back(tmp); } } //back mid left tmp = Vector2i(slotPos.x - 1 , slotPos.y - 2); if(tmp.x >= 0 && tmp.y >= 0){ if(_canHighlightSlot(gameBoard,tmp) != NOT_HIGHLIGHTED){ possibleLocations.push_back(tmp); } } //back mid right tmp = Vector2i(slotPos.x - 1, slotPos.y + 2); if(tmp.x >= 0 && tmp.y < ChessBoard::BOARD_WIDTH){ if(_canHighlightSlot(gameBoard,tmp) != NOT_HIGHLIGHTED){ possibleLocations.push_back(tmp); } } //front top left tmp = Vector2i(slotPos.x + 2 , slotPos.y - 1); if(tmp.x < ChessBoard::BOARD_HEIGHT && tmp.y >= 0){ if(_canHighlightSlot(gameBoard,tmp) != NOT_HIGHLIGHTED){ possibleLocations.push_back(tmp); } } //front top right tmp = Vector2i(slotPos.x + 2, slotPos.y + 1); if(tmp.x < ChessBoard::BOARD_WIDTH && tmp.y < ChessBoard::BOARD_WIDTH ){ if(_canHighlightSlot(gameBoard,tmp) != NOT_HIGHLIGHTED){ possibleLocations.push_back(tmp); } } //back bottom left tmp = Vector2i(slotPos.x - 2, slotPos.y - 1); if(tmp.x >= 0 && tmp.y >= 0){ if(_canHighlightSlot(gameBoard,tmp) != NOT_HIGHLIGHTED){ possibleLocations.push_back(tmp); } } //back bottom right tmp = Vector2i(slotPos.x - 2 , slotPos.y + 1); if(tmp.x >= 0 && tmp.y < ChessBoard::BOARD_WIDTH){ if(_canHighlightSlot(gameBoard,tmp) != NOT_HIGHLIGHTED){ possibleLocations.push_back(tmp); } } return possibleLocations; }
Color colorFromString(const char *name_or_rgb) { if (name_or_rgb[0] == '#') { unsigned c; if (sscanf(name_or_rgb, "#%X", &c)!=1) return Color(255,255,255,255); return Color(c>>16, (c>>8)&0xFF, c&0xFF, 0xFF); } else if (strncmp(name_or_rgb, "rgb(", 4)==0) {
void Animation::Load() { int frameAnimationIndex = -1; vector<FrameList> frameLists; string currentState = ""; float currentAnimationDuration = 0.0f; int currentWidth = 0; int currentHeight = 0; bool legacySupport = true; string data = FileUtil::Read(path); int endline = 0; do { endline = (int)data.find("\n"); string line = data.substr(0, endline); // NOTE: Support older animation files until we upgrade completely... if (line.find("VERSION") != string::npos) { string version = ValueOf("VERSION", line); if (version != "1.0") legacySupport = false; } else if (line.find("animation") != string::npos) { if (!frameLists.empty()) { //std::cout << "animation total seconds: " << sf::seconds(currentAnimationDuration).asSeconds() << "\n"; //std::cout << "animation name push " << currentState << endl; animations.insert(std::make_pair(currentState, frameLists.at(frameAnimationIndex))); currentAnimationDuration = 0.0f; } string state = ValueOf("state", line); currentState = state; if (legacySupport) { string width = ValueOf("width", line); string height = ValueOf("height", line); //std::cout << "animation name in " << currentState << endl; currentWidth = atoi(width.c_str()); currentHeight = atoi(height.c_str()); } frameLists.push_back(FrameList()); frameAnimationIndex++; } else if (line.find("frame") != string::npos) { string duration = ValueOf("duration", line); float currentFrameDuration = (float)atof(duration.c_str()); int currentStartx = 0; int currentStarty = 0; float originX = 0; float originY = 0; if (legacySupport) { string startx = ValueOf("startx", line); string starty = ValueOf("starty", line); currentStartx = atoi(startx.c_str()); currentStarty = atoi(starty.c_str()); } else { string x = ValueOf("x", line); string y = ValueOf("y", line); string w = ValueOf("w", line); string h = ValueOf("h", line); string ox = ValueOf("originx", line); string oy = ValueOf("originy", line); currentStartx = atoi(x.c_str()); currentStarty = atoi(y.c_str()); currentWidth = atoi(w.c_str()); currentHeight = atoi(h.c_str()); originX = (float)atoi(ox.c_str()); originY = (float)atoi(oy.c_str()); } currentAnimationDuration += currentFrameDuration; if (legacySupport) { frameLists.at(frameAnimationIndex).Add(currentFrameDuration, IntRect(currentStartx, currentStarty, currentWidth, currentHeight)); } else { frameLists.at(frameAnimationIndex).Add(currentFrameDuration, IntRect(currentStartx, currentStarty, currentWidth, currentHeight), sf::Vector2f(originX, originY)); } } data = data.substr(endline + 1); } while (endline > -1); // One more addAnimation to do if file is good if (frameAnimationIndex >= 0) { animations.insert(std::make_pair(currentState, frameLists.at(frameAnimationIndex))); } }
int main(int argc, char **argv) { vector<string> args; for(unsigned i = 1; i < (unsigned)argc; ++i) args.push_back((string)argv[i]); srand(time(NULL)); if(inVector(args, (string)"--help") || inVector(args, (string)"-h")) { outputHelp(cout); return 0; } bool loadSystem = false; string systemName = ""; for(unsigned i = 0; i < args.size(); ++i) { if(args[i] == (string)"--load") { if(i != args.size() - 1) { loadSystem = true; systemName = args[i + 1]; args.erase(args.begin() + i); args.erase(args.begin() + i); i -= 2; } else { cerr << "psystem: --load requires argument" << endl; return 2; } } } if(!args.empty()) { cerr << "psystem: invalid argument passed" << endl; outputHelp(cerr); return 1; } unsigned windowWidth = 800, windowHeight = 600; const ldouble scaleFactor = 10.0, minPSize = 0.3, maxPSize = 3.6; string windowTitle = "psystem"; RenderWindow window(VideoMode(windowWidth, windowHeight), windowTitle, sf::Style::Close); window.setFramerateLimit(60); View view; view.setSize(windowWidth / scaleFactor, windowHeight / scaleFactor); unsigned steps = 100; ParticleSystem psystem(1.0 / 60.0 / steps); ldouble psize = 3.6 / 4, psizeDelta = 0.1; if(loadSystem) { psystem.load(systemName); if(psystem.size() < 1) { return 3; } } else { unsigned ipcount = rand() % 5 + 3; psize = 3.6 / ipcount; for(unsigned i = 0; i < ipcount; ++i) { ldouble radians = (rand() % 360) * M_PI/180.0; psystem.push(Particle(cos(radians)*16.0, sin(radians)*16.0, psize)); } } bool done = false, mode = true, run = true; while(!done && window.isOpen()) { Event event; while(window.pollEvent(event)) { if(event.type == Event::Closed) window.close(); if(event.type == Event::KeyPressed) { if(event.key.code == Keyboard::Escape) done = true; if(event.key.code == Keyboard::Space) mode = !mode; if(event.key.code == Keyboard::P) run = !run; } if(event.type == Event::MouseButtonPressed) { // ConvertCoords -> mapPixelToCoords or mapCoordsToPixel sf::Vector2i mouseButtonLoc(event.mouseButton.x, event.mouseButton.y); ldouble mx = window.mapPixelToCoords(mouseButtonLoc, view).x, my = window.mapPixelToCoords(mouseButtonLoc, view).y; if(event.mouseButton.button == Mouse::Left) { psystem.push(Particle(mx, my, psize)); } else if(event.mouseButton.button == Mouse::Right) { for(unsigned p = 0; p < psystem.size(); ++p) { Particle cp = psystem.get(p); ldouble dx = mx - cp.px, dy = my - cp.py, distance = sqrt(dx*dx + dy*dy); if(distance < cp.radius) { psystem.erase(p); break; } } } } if(event.type == Event::MouseWheelMoved) { psize += event.mouseWheel.delta * psizeDelta; if(psize < minPSize) psize = minPSize; if(psize > maxPSize) psize = maxPSize; } } if(run) for(unsigned i = 0; i < steps/2; ++i) psystem.update(); window.clear(Color::White); if(mode) { view.setCenter(psystem.getX(), psystem.getY()); } else if(psystem.size() > 0) { view.setCenter(psystem.get(0).px, psystem.get(0).py); } window.setView(view); psystem.draw(window); window.display(); } if(window.isOpen()) window.close(); return 0; }