Skip to content
Snippets Groups Projects
Commit 3ed6e5f2 authored by BOVO Pascal's avatar BOVO Pascal
Browse files

Using launch path to find assets

parent b3a8d361
Branches
No related tags found
No related merge requests found
......@@ -4,8 +4,9 @@
const char* chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&/*()-_+=: ";
Display::Display(Solution *p_solution) : window(nullptr), renderer(nullptr), background(nullptr) {
Display::Display(Solution *p_solution, std::string pathToExe) : window(nullptr), renderer(nullptr), background(nullptr) {
_sol = p_solution;
_pathToExe = pathToExe;
}
bool Display::init(const std::string& title) {
......@@ -28,27 +29,27 @@ bool Display::init(const std::string& title) {
}
// Load the background texture
background = loadTexture("assets/background.png");
background = loadTexture(_pathToExe+"assets/background.png");
if (!background) {
std::cerr << "Failed to load background texture" << std::endl;
return false;
}
perso = loadTexture("assets/Perso.png");
perso = loadTexture(_pathToExe+"assets/Perso.png");
if (!perso) {
std::cerr << "Failed to load perso texture" << std::endl;
return false;
}
play = loadTexture("assets/Play.png");
play = loadTexture(_pathToExe+"assets/Play.png");
if (!perso) {
std::cerr << "Failed to load play texture" << std::endl;
return false;
}
pause = loadTexture("assets/Pause.png");
pause = loadTexture(_pathToExe+"assets/Pause.png");
if (!perso) {
std::cerr << "Failed to load pause texture" << std::endl;
return false;
}
reload = loadTexture("assets/Reload.png");
reload = loadTexture(_pathToExe+"assets/Reload.png");
if (!reload) {
std::cerr << "Failed to load reload texture" << std::endl;
return false;
......@@ -56,7 +57,7 @@ bool Display::init(const std::string& title) {
slider = { buttonsSize*3, backgroundHeight, backgroundWidth - (buttonsSize*3 + nbWidth*2), buttonsSize, 20, 0, false }; // x, y, width, height, handleWidth, initial value
font24Map = loadTexture("assets/Courier_New24.bmp");
font24Map = loadTexture(_pathToExe+"assets/Courier_New24.bmp");
if (!font24Map) {
std::cerr << "Failed to load font texture" << std::endl;
return false;
......@@ -64,7 +65,7 @@ bool Display::init(const std::string& title) {
SDL_QueryTexture(font24Map, NULL, NULL, &font24Width, &font24Height);
font24Width /= strlen(chars);
font18Map = loadTexture("assets/Courier_New18.bmp");
font18Map = loadTexture(_pathToExe+"assets/Courier_New18.bmp");
if (!font18Map) {
std::cerr << "Failed to load font texture" << std::endl;
return false;
......@@ -83,7 +84,7 @@ SDL_Texture* Display::loadTexture(const std::string& path) {
return texture;
}
void Display::render(const Solution& solution) {
void Display::render() {
if (playing){
if ((clock() - lastFrame) > CLOCKS_PER_SEC*0.3){
_sol->setCurrentStateIndex(_sol->getCurrentStateIndex()+1);
......@@ -189,7 +190,7 @@ void Display::renderText(const std::string& text, SDL_Rect& rect, int size) {
const char* ctext = text.c_str();
for (int i = 0 ; i<strlen(ctext) ; i++){
for (size_t i = 0 ; i<strlen(ctext) ; i++){
if (ctext[i] =='\n'){
cursor.x = rect.x;
cursor.y += fontHeight;
......@@ -226,15 +227,12 @@ void Display::renderText(const std::string& text, SDL_Rect& rect, int size) {
rect.h = cursor.y + fontHeight - rect.y;
}
void Display::measureText(const std::string& text, SDL_Rect& rect, int size) {
SDL_Texture* fontMap;
int fontWidth;
int fontHeight;
if (size == 18){
fontMap = font18Map;
fontWidth = font18Width;
fontHeight = font18Height;
} else {
fontMap = font24Map;
fontWidth = font24Width;
fontHeight = font24Height;
}
......@@ -243,7 +241,7 @@ void Display::measureText(const std::string& text, SDL_Rect& rect, int size) {
const char* ctext = text.c_str();
for (int i = 0 ; i<strlen(ctext) ; i++){
for (size_t i = 0 ; i<strlen(ctext) ; i++){
if (ctext[i] =='\n'){
cursor.x = rect.x;
cursor.y += fontHeight;
......@@ -285,9 +283,9 @@ void Display::renderActions(){
std::vector<std::string> actions = _sol->getActionsStrings();
SDL_Rect currentCell = {.x=backgroundWidth, .y=-actionsScroll, .w=actionsColWidth, .h=500};
for (int i = 0;i<actions.size();i++){
for (size_t i = 0;i<actions.size();i++){
measureText(actions[i], currentCell, 18);
if (i == _sol->getCurrentStateIndex()-1){
if ((int)i == _sol->getCurrentStateIndex()-1){
SDL_SetRenderDrawColor(renderer, 0, 255, 255, 255);
} else {
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
......@@ -316,7 +314,7 @@ void Display::handleEvents(bool& running) {
int mouseY = event.button.y;
// Check if the mouse is on the handle
int handleX = slider.x + (slider.value * (slider.width - slider.handleWidth));
//int handleX = slider.x + (slider.value * (slider.width - slider.handleWidth));
//if (mouseX >= handleX && mouseX <= handleX + slider.handleWidth &&
if (mouseX >= slider.x && mouseX <= slider.x + slider.width &&
mouseY >= slider.y && mouseY <= slider.y + slider.height)
......
......@@ -17,9 +17,9 @@ struct Slider {
};
public:
Display(Solution *p_solution);
Display(Solution *p_solution, std::string pathToExe);
bool init(const std::string& title);
void render(const Solution& solution);
void render();
void handleEvents(bool& running);
void cleanup();
......@@ -34,6 +34,7 @@ private:
private:
Solution *_sol;
std::string _pathToExe;
SDL_Window* window;
SDL_Renderer* renderer;
......
CXX=g++
CXXFLAGS=-Wall -Wextra `sdl2-config --cflags`
CXXFLAGS=-Wall -Wextra -Wno-missing-field-initializers `sdl2-config --cflags`
LDFLAGS=`sdl2-config --libs` -lSDL2_image -lstdc++
overcooked: main.o Solution.o Display.o
......
......@@ -97,6 +97,7 @@ std::string Solution::_actionTypeToString(ActionType_t act){
case Yes: return "Yes";
case Stop: return "Stop";
}
return "";
}
Solution::Agent_t Solution::_agentFromString(std::string str, std::string &err){
......@@ -133,6 +134,7 @@ std::string Solution::_objectToString(Object_t object){
case SoupFromRaw: return "SoupFromRaw";
case SoupAlreadyReady: return "SoupAlreadyReady";
}
return "";
}
Solution::Location_t Solution::_locationFromString(std::string str, std::string &err){
if (str == "faraway") return Location_t::Faraway;
......@@ -156,6 +158,7 @@ std::string Solution::_locationToString(Location_t loc){
case CookLoc: return "CookLoc";
case DeliveryLoc: return "DeliveryLoc";
}
return "";
}
......@@ -303,11 +306,12 @@ Solution::Point_t Solution::_coordinatesOfLocation(Location_t loc){
case DeliveryLoc:
return (Point_t){.x=5,.y=0};
}
return (Point_t){.x=0,.y=0};
}
std::vector<std::string> Solution::getActions(){
std::vector<std::string> output;
for (int i = 0 ; i<_actions.size() ; i++){
for (size_t i = 0 ; i<_actions.size() ; i++){
output.push_back("#"+ std::to_string(i) + " " + _actionToString(_actions[i]));
}
return output;
......
......@@ -11,6 +11,15 @@ int main(int argc, char *argv[])
return -1;
}
std::string pathToExe = argv[0];
std::size_t lastSlash = pathToExe.find_last_of('/');
if (lastSlash == std::string::npos){
pathToExe = "";
} else {
pathToExe = pathToExe.substr(0, lastSlash+1);
std::cerr << "pathToExe " << pathToExe<<std::endl;
}
Solution solution(argv[1]);
if (!solution.load()) {
std::cerr << "Failed to load solution" << std::endl;
......@@ -18,7 +27,7 @@ int main(int argc, char *argv[])
}
Display display(&solution);
Display display(&solution, pathToExe);
if (!display.init("Overcooked Animation")) {
std::cerr << "Failed to initialize display" << std::endl;
return -1;
......@@ -32,7 +41,7 @@ int main(int argc, char *argv[])
display.handleEvents(running); // Handle events like play, pause, quit
if (clock() - lastRender > CLOCKS_PER_SEC / 60){
//unsigned long startRender = clock();
display.render(solution); // Render the current frame
display.render(); // Render the current frame
//std::cerr<<"renderTime "<<(clock()-startRender)*1000./CLOCKS_PER_SEC<<"ms"<<std::endl;
} else {
SDL_Delay(5);
......
......@@ -68,7 +68,8 @@ plan cooking_domain.hddl cooking_problem.hddl debug > plan.txt
```
cd OvercookedVisu
make
./overcooked ../plan.txt
cd ..
./OvercookedVisu/overcooked plan.txt
```
## Repository Overview
......@@ -87,8 +88,7 @@ open 2 files:
```
plan cooking_domain.hddl cooking_problem.hddl debug > plan.txt
cd OvercookedSimu
./overcooked ../plan.txt
./OvercookedVisu/overcooked plan.txt
```
- [ ] now, comment 1 line and uncomment 1 line to make the agent start "far away" from the delivery location and check that planning process will fail.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment