Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 74 75 [76] 77 78 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 903451 times)

Aqizzar

  • Bay Watcher
  • There is no 'U'.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1125 on: February 01, 2012, 10:15:21 pm »

Man, I never would have thought of this structuring stuff.  Game as an object, and separate gamestates?  Makes sense I guess, but I gotta be honest, I kinda like the ad-hoc system I had going.  Except my system would only be acceptable for turn-based gaming, and couldn't animate anything more complicated than like Nethack does.

Does look like I'll have to archive my code, and either start over from scratch or make a complete rebuild.  I'll probably do the rebuild, because God knows I'm lazy.  I'll save the structure ideas for another game, I think.
Logged
And here is where my beef pops up like a looming awkward boner.
Please amplify your relaxed states.
Quote from: PTTG??
The ancients built these quote pyramids to forever store vast quantities of rage.

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1126 on: February 01, 2012, 10:31:29 pm »

Eh, I'm willing to bet that most of your code can be carried over to a new project without too much effort. The biggest problem that you might have is separating all the update logic from the draw logic. If I were you, I would just delete the draw logic, because it is redundant anyway.

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1127 on: February 02, 2012, 12:04:19 am »

Thanks Mego, that was exceptionally helpful.

Code: [Select]
throw Helpful();

Couldn't resist. Also I should do more work on my game project (that I started over a year ago) sometime soon. I think I'm on re-write #4 right now. I keep failing YAGNI and adding in stuff I may never need. I semi-implemented a mixture of features from Java and Python that will make my life somewhat easier but that are extremely unnecessary. Oh well.

Code: [Select]
#include <exception>
#include <string>
#include <cstdarg>

#include <boost/any.hpp>

using namespace std;

class IllegalArgumentException : public exception {
public:
    const string arg;
    const string what() {
        return arg;
    }
    IllegalArgumentException(string s) : arg(s) {}
};

string join(vector<string> v) {
    string ret;
    for(vector<string>::iterator it = v.begin(); it != v.end(); it++) {
        ret += *it + "\n";
    }
    return ret;
}

vector<string> split(string s) {
    vector<string> ret;
    for(size_t i = s.find('\n'); i != string::npos; i = s.find('/n', i+1)) {
        ret.push_back(s.substr(i+1, s.find('\n', i+1)-i-1));
    }
    return ret;
}

template<class T> string toString(T arg) {
    return toString(boost::any(arg));
}

template<class T> string toString<vector<T> >(vector<T> arg) {
    string ret;
    try {
        for(auto it = arg.begin(); it != arg.end(); it++) {
            ret += toString(*it);
            if(it != arg.end()-1) ret += " ";
        }
        return ret;
    } catch(...) {
        throw IllegalArgumentException("Error: vector contents cannot be cast to a string.");
    }
}

template<> string toString<string>(string arg) {
    return arg;
}

template<> string toString<boost::any>(boost::any arg) { // The one thing Java did right
    try {
        return boost::any_cast<string>(arg);
    } catch(...) {
        throw IllegalArgumentException("Error: argument 0 cannot be cast to a string.");
    }
}

string format(string raw, ...) { // Python is good stuff
    if(!raw.contains("{0}")) return raw;
    int max;
    for(int i = 1; i; i++) {
        if(!raw.contains(string("{") + itoa(i) + string("}"))) max = i-1;
    }
    va_list args;
    va_start(args, max+1);
    boost::any s;
    for(int i = 0; i < max; i++) {
        s = va_arg(args, string);
        raw.replace(raw.find(string("{") + itoa(i) + string("}")), 3, boost::any_cast<string>(s));
    }
    va_end(args);
    return raw;
}

Awesome, but impractical.

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1128 on: February 02, 2012, 02:29:37 am »

This is getting rather frustrating... Why can libtcod do pallet swaps with SDL so fast, but not me? how does it draw to its screen?

alway

  • Bay Watcher
  • 🏳️‍⚧️
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1129 on: February 02, 2012, 02:54:52 am »

This is getting rather frustrating... Why can libtcod do pallet swaps with SDL so fast, but not me? how does it draw to its screen?
According to wiki, SDL's rendering is done with a variety of APIs, depending on the OS used. That said, its pallet swaps would probably consist of simply changing the texture being sent to the GPU. What sort of processing goes into that would vary based on how they implemented it; which I can't comment on as I've never personally used libtcod. For fastest results, they probably keep swaps to a minimum through batching, if not going as far as concatenating the textures into a single texture to avoid swapping textures altogether. If implemented properly, pallet swapping is trivial as far as the computations added per frame. If implemented inefficiently, it becomes a massive drain. In particular, I would imagine libtcod doesn't give anything close to direct graphics access, as it's a roguelike library, not a graphics API, and as such you won't have anything near the required tools to implement palette swapping efficiently without resorting to extreme measures like concatenating the palettes manually and adjusting your code to match it.
Logged

RulerOfNothing

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1130 on: February 02, 2012, 02:58:45 am »

This is getting rather frustrating... Why can libtcod do pallet swaps with SDL so fast, but not me? how does it draw to its screen?
If you're talking about how it can put console-like characters on the screen in 24-bit colour, I would suppose that it loads the graphic containing the characters as a 8-bit surface, then uses SDL_SetColor to change the colormap.
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1131 on: February 02, 2012, 03:01:11 am »

If you're talking about how it can put console-like characters on the screen in 24-bit colour, I would suppose that it loads the graphic containing the characters as a 8-bit surface, then uses SDL_SetColor to change the colormap.
Hmm, I must be doing it pitifully poorly to get such horrible performance. I wish there was half decent documentation on best practice for this sort of thing.

Errol

  • Bay Watcher
  • Heaven or Hell, Duel 1 -- Let's Rock!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1132 on: February 02, 2012, 10:59:43 am »

JAVA, Y U SO COMPLICATED WITH PICTURES?

Sorry, I'm just really worked up over this. For a substantial amount of time now, I have been trying to understand how to insert image graphics into a plain old frame. However, there seems to be nobody at all who can explain this in an Errol-understandable format. Moreover, most solutions have been with a JLabel. I fear that this wouldn't be practical for me, as it would entail setting up at least 450 JLabels (tile based graphics, yeah)

So, please, how do I make a program where I can insert an image from a file and get a nice little image object for Graphics to use?
Logged
Girls are currently preparing signature, please wait warmly until it is ready.

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1133 on: February 02, 2012, 11:14:45 am »

JAVA, Y U SO COMPLICATED WITH PICTURES?

Sorry, I'm just really worked up over this. For a substantial amount of time now, I have been trying to understand how to insert image graphics into a plain old frame. However, there seems to be nobody at all who can explain this in an Errol-understandable format. Moreover, most solutions have been with a JLabel. I fear that this wouldn't be practical for me, as it would entail setting up at least 450 JLabels (tile based graphics, yeah)

So, please, how do I make a program where I can insert an image from a file and get a nice little image object for Graphics to use?

Woah!  Relax, chief!  BufferedImage is your friend.  Probably the easiest way to get an image on screen is to override JPanel.  Give it a BufferedImage or something as an instance variable to have it take in that image from the constructor.  Override paintComponent(Graphics) to run super() and then drawImage().  If you have further questions, I'm probably the Java guy to ask.

EDIT: I'll write up a quick lesson on this after I'm done with class this afternoon.
« Last Edit: February 02, 2012, 11:17:07 am by Stargrasper »
Logged

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: if self.isCoder(): post() #Programming Thread
« Reply #1134 on: February 02, 2012, 11:16:38 am »

JAVA, Y U SO COMPLICATED WITH PICTURES?

Sorry, I'm just really worked up over this. For a substantial amount of time now, I have been trying to understand how to insert image graphics into a plain old frame. However, there seems to be nobody at all who can explain this in an Errol-understandable format. Moreover, most solutions have been with a JLabel. I fear that this wouldn't be practical for me, as it would entail setting up at least 450 JLabels (tile based graphics, yeah)

So, please, how do I make a program where I can insert an image from a file and get a nice little image object for Graphics to use?

I have something at home, if someone else doesn't give you enough info before then I will post it up later today.
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1135 on: February 02, 2012, 11:22:44 am »

-game state handler design-

I have always had the problem that this design has lots of weaknesses, for example state transition code and passing data from one state to the other.
For example, you have to make absolutely horrible contortions if you want to write a simple pause menu that pauses a game, allows the user to set a setting, and then continues the game.

I've designed an even better design that I now always use:

Code: [Select]
// class State { };
Yes, exactly. States are normally just not similar enough to all be handled through the same base class interface. If I want to switch from an old state to a new state, I just create a new instance of this new state (with some options), then have the new state run. After the new state has finished running, the old state can ask the new state about what's happened, then do something with the information, like running another state.
Example:

Code: [Select]
class QueryState {
public:
QueryState(string s): question(s) {};
void run_query();
string get_result();
private:
string question;
string result;
}

void QueryState::run_query() {
cout << question << flush;
cin >> result;
}

string QueryState::get_result() {
return result;
}

class MainState {
public:
void run();
}

void MainState::run() {
QueryState q("What's your favourite colour? ");
q.run_query();
if(q.get_result() != "Orange") {
cout << "Now I'm sad." << endl;
}
}

int main() {
MainState m;
m.run();
return 0;
}

Nifty, right? Try doing that with your much-too-rigid state model. Every state here has the freedom to define its own interface, states can initialize and run each other (So Fucking Useful) and exchange data so much easier (the child state instance belongs to the parent state instance, therefore the parent instance can call the child instance's methods). Also, this model is very easily extendable and can easily be enhanced for even more awesome effects. For example, say you have a graphical interface, and you need some states to handle input events. Then you can use the following:

Code: [Select]
#include "gui.h"

class GState {
protected:
void GRun();
virtual void GOnInit() {};
virtual void GOnEvent(GUI::Event * event) { if (event->isQuitEvent()) return;};
virtual void GOnLoop() {};
virtual void GOnRender() {};
virtual void GOnCleanup() {};
void GExit() {running = false};
private:
bool running;
void EventLoop();
}

void GState::GRun() {
running = true;
GOnInit();
while (running) EventLoop();
GOnCleanup();
return 0;
}

void GState::EventLoop() {
GUI::Event Event;
while(GUI::instance()->GetNextEvent(&Event)) {
GOnEvent(&Event);
if (!running) return;
}
GOnLoop();
if (!running) return;
GOnRender();

}
You'll probably recognize this code, as you've probably seen classes very similar to this before, but under a different name, such as App or Game or something. But here, every State that needs an event handler can just inherit from GState and enjoy all its features. Note that there are no public methods and attributes, so the GState class is simply a code template, and other States cannot be held liable for inheriting from GState.
But how can you actually use this GState class?
Easy: Any class GDerived that derives from GState can overload some of the GOnSomething() methods to specify its own behaviour, and if an instance of GDerived is asked to run, then it can call GRun() on itself and continue running with an event handler. Wow.
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1136 on: February 02, 2012, 11:47:46 am »

For example, you have to make absolutely horrible contortions if you want to write a simple pause menu that pauses a game, allows the user to set a setting, and then continues the game.
What? No you don't. If the paused state has an attribute
Code: [Select]
GameState priorState;Then there is all your data encapsulated in a single place. Keeping track of data is as easy as that! It can then easily call on the update and draw methods if it needs, so for example.
Spoiler (click to show/hide)
Now see how easy that was. Gotta think in objects man!
Also, in mine States are responsible for the creation of other states, then pass it into the Game object. So in my character creation state, I would make a new level state, and pass in the newly made player as an attribute.

I seriously don't see any practical application your can do, that mine can't do stricter.

Errol

  • Bay Watcher
  • Heaven or Hell, Duel 1 -- Let's Rock!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1137 on: February 02, 2012, 11:49:15 am »

Stargrasper, Nadaka, thanks in advance :)
Logged
Girls are currently preparing signature, please wait warmly until it is ready.

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1138 on: February 02, 2012, 02:02:07 pm »

I seriously don't see any practical application your can do, that mine can't do stricter.
That's probably correct, but strict is not the same as elegant. A state manager that supports everything that you want it to be able to do will be ugly as hell, juggling with pointers and/or passing large game state objects, with extra conditions and insane amounts of extra code just to handle dynamic deallocation. It's easier and much safer to handle states decentrally.

I seriously don't see any practical application your can do, that mine can't do more easily and readably.
Logged

MaximumZero

  • Bay Watcher
  • Stare into the abyss.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1139 on: February 02, 2012, 02:11:12 pm »

Hey, for Java compiling from the console (cmd), what am I supposed to set the path to? I remember doing this in Java class, but I can't get it to work on my home pc.
Logged
  
Holy crap, why did I not start watching One Punch Man earlier? This is the best thing.
probably figured an autobiography wouldn't be interesting
Pages: 1 ... 74 75 [76] 77 78 ... 796