<?xml version="1.0" encoding="iso-8859-1" ?>
<rss version="2.0">
<channel>
	<title>Dream.In.Code Community Blog List</title>
	<link>http://www.dreamincode.net/forums/index.php?automodule=blog</link>
	<description>Community Blog List Syndication</description>
	<pubDate>Fri, 20 Nov 2009 16:08:35 -0600</pubDate>
	<webMaster>admin@dreamincode.net (Dream.In.Code)</webMaster>
	<generator>Invision Community Blog</generator>
	<ttl>60</ttl>
	<item>
		<title><![CDATA[cout&lt;&lt;alias120sBlog - Here we go..]]></title>
		<link><![CDATA[http://www.dreamincode.net/forums/index.php?automodule=blog&blogid=801&showentry=1924]]></link>
		<category></category>
		<description><![CDATA[I will introduce myself by stating that i am a novice programmer. So if you are looking for advanced topics this is not where you want to be   <img src="http://www.dreamincode.net/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /> <br />Critics for posted code are more than welcome though, I believe it is impossible to become a better programmer without recognizing your mistakes.  <br /><br />The intent of this blog is to give examples of novice topics using C++ as the language. Not only do i hope this will assist other learning coders, <br />but it gives me an excuse to practice writing more code. <br /><br />I'll start out by posting a little piece here that will introduce many topics you will see when you first start coding; to include assigning user input to <br />a variable,  using control structures such as do-while loops and 'if' statments and passing parameters to functions. Any question are welcome, and there <br />are plenty more to come. Enjoy!<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--><br />#include&#60;iostream&#62;<br />using namespace std;<br /><br />// Below we are declaring our functions prior to our main. <br />// This simply allows for our functions to be positioned below 'main'. <br /><br />int add &#40;int x, int y&#41;;<br />int sub &#40;int x, int y&#41;;<br />int mult&#40;int x, int y&#41;;<br />int divi&#40;int x, int y&#41;;<br /><br />//The start of our 'main' function<br />int main &#40;&#41; {<br /><br />int x;<br />int y;<br />int choice;&nbsp;&nbsp;//Declaring variables that will allow the user to input the<br />int sum;&nbsp;&nbsp;&nbsp;&nbsp; //numbers to manipulate, along with their chosen calculation.<br /><br />cout&#60;&#60;&#34;Welcome to the SCP &#40;Simple Calculator Program&#41;.&#34;;<br />cout&#60;&#60;end1;<br /><br />do {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //Starting a do-while loop that will allow the user repeated use of the program until<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //they are done. <br /><br />cout&#60;&#60;&#34;Please enter a 1 to Add, a 2 to Subtract, a 3 to Multiply, or a 4 to divide. Enter 0 to quit&#34;;<br />cin&#62;&#62;choice;&nbsp;&nbsp; //Inputing the desired calculation<br /><br /> if &#40; choice == 1 &#41; {<br /><br />cout&#60;&#60;&#34;Please enter the numbers that you would like to add.&#34;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//You could use a 'switch' statement <br />cin&#62;&#62;x;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//that would be more efficient, but we<br />cin&#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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//like to perform. <br />}<br /><br /> if &#40; choice == 2 &#41; {<br /><br />cout&#60;&#60;&#34;Please enter the numbers that you would like to subtract.&#34;;<br />cin&#62;&#62;x;<br />cin&#62;&#62;y;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //'cin' takes the users input, and then 'sub&#40;&#41;' passes that input<br />sub&#40;x, y&#41;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//to our seperate function that we declared in the begining of the program. <br />cout&#60;&#60;&#34;Your total is&#58; &#34;, sum;&nbsp;&nbsp;&nbsp;&nbsp;//Our function returns the calculation, and 'sum' outputs the end result. <br /> <br /> }<br /><br /> if &#40; choice == 3 &#41; {<br /><br />cout&#60;&#60;&#34;Please enter the numbers that you would like to multiply.&#34;;<br />cin&#62;&#62;x;<br />cin&#62;&#62;y;<br />mult&#40;x, y&#41;;<br />cout&#60;&#60;&#34;Your total is&#58; &#34;, sum;<br /> <br /> }<br /><br /> if &#40; choice == 4 &#41; {<br /><br />cout&#60;&#60;&#34;Please enter the numbers that you would like to divide.&#34;;<br />cin&#62;&#62;x;<br />cin&#62;&#62;y;<br />divi&#40;x, y&#41;;<br />cout&#60;&#60;&#34;Your total is&#58; &#34;, sum;<br /> <br /> }<br /><br /> if &#40; choice == 0 &#41; {<br /><br />cout&#60;&#60;&#34;Thank you for using SCP, Goodbye.&#34;;<br />cin.get&#40;&#41;;<br /> <br /> }<br /><br />else {<br />cout&#60;&#60;&#34;You did not select a valid option.&#34;;<br />&nbsp;&nbsp;}<br /><br /> }while &#40;choice != 0&#41;;<br /><br />}<br /><br /><br /><br />int add&#40;int x, int y&#41; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//Here are our functions that perform the actual<br />int sum;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //calculations. The user input is passed using the <br />sum = x + y;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //function call in the main function. These sub functions recieve <br />return &#40;sum&#41;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//the input using '&#40;int x, int y&#41;' and perform the designated<br />}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //calculation. They assign the <br /><br />int sub&#40;int x, int y&#41; {<br />int sum;<br />sum = x - y;<br />return &#40;sum&#41;;<br />}<br /><br />int multi&#40;int x, int y&#41; {<br />int sum;<br />sum = x * y;<br />return &#40;sum&#41;;<br />}<br /><br />int divi&#40;int x, int y&#41;&nbsp;&nbsp;{<br />int sum;<br />sum = x / y;<br />return &#40;sum&#41;;<br />}<br /><!--c2--></div><!--ec2--><br />]]></description>
		<pubDate>Fri, 20 Nov 2009 14:49:53 -0600</pubDate>
		<guid><![CDATA[http://www.dreamincode.net/forums/index.php?automodule=blog&blogid=801&showentry=1924]]></guid>
	</item>
	<item>
		<title>Stuck in an Infiniteloop - Advanced Lua - Part III</title>
		<link><![CDATA[http://www.dreamincode.net/forums/index.php?automodule=blog&blogid=324&showentry=1923]]></link>
		<category></category>
		<description><![CDATA[<!--sizeo:5--><span style="font-size:18pt;line-height:100%"><!--/sizeo--><b><u>Advanced Lua- Part III</u></b><!--sizec--></span><!--/sizec--><br /><br /><b><u><!--sizeo:2--><span style="font-size:10pt;line-height:100%"><!--/sizeo-->Prerequisites:<!--sizec--></span><!--/sizec--></u></b><br /><br />A solid understanding of Lua. It is recommended, but certainly not required, that you read my previous tutorials on the subject, which can be found <a href="http://www.dreamincode.net/forums/showtopic98241.htm" target="_blank">starting here(intro)</a> and <a href="http://www.dreamincode.net/forums/showtopic121185.htm" target="_blank">here(advanced)</a>. Since this tutorial covers how to embed Lua in other languages a grasp on programming fundamentals will also be required. In particular, be familiar with C/C++/Java. I will not be explaining things like control or data structures. Having that said, let's begin:<br /><br /><b><!--sizeo:2--><span style="font-size:10pt;line-height:100%"><!--/sizeo--><u>Getting in Bed With Lua:</u><!--sizec--></span><!--/sizec--></b><br /><br />Up until this point, all of the Lua we've been using has been stand alone. Suffice it to say that this is not the intention or purpose of the language. Not to say it isn't or cannot be useful by itself, it truly shines when embedded in another language. (A testament to this is Lua's widespread usage in the video game industry). Those with prior programming experience will immediately note that Lua is a library that is used to bring Lua functionality into a program. <br /><br /><br /><i>From Programming in Lua, 2nd Edition:</i><br /><!--quoteo--><div class='quotetop'>QUOTE</div><div class='quotemain'><!--quotec--><br />[The] ability to be used as a library to extend an application is what makes Lua an <i>extension language</i>.<br />At the same time a program that uses Lua can register new functions in the Lua environment...This is what makes Lua an <i>extensible language</i>. <br /><!--QuoteEnd--></div><!--QuoteEEnd--><br /><br />At running the risk of a gross oversimplification, Lua is quite flexible. Given the definitions above, there are varying instances where Lua and [another programming language] will switch roles between application and library depending on the circumstances. We could go down a rat hole about this, but as long as you can recognize when each is playing the part [application or library, etc...] then you'll be fine. <br /><br /><br />A quick example of opening a basic Lua interpreter in C:<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--><br />#include &#60;stdio.h&#62;<br />#include &#60;string.h&#62;<br /><br />/* If using a ANSI C compile, just include the libs<br />I'm using VS 2005 and compiling from a C++ environment requires extern &#34;C&#34;<br />*/<br />#ifdef __cplusplus<br />extern &#34;C&#34; {<br />#endif<br />#include &#60;lua.h&#62;<br />#include &#60;lauxlib.h&#62;<br />#include &#60;lualib.h&#62;<br />#ifdef __cplusplus<br />}<br />#endif<br /><br />int main&#40;&#41;<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;char buff&#91;256&#93;;<br />&nbsp;&nbsp;&nbsp;&nbsp;int error;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lua_State *L = luaL_newstate&#40;&#41;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*open a new lua state*/<br />&nbsp;&nbsp;&nbsp;&nbsp;luaL_openlibs&#40;L&#41;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*open standard libraries*/<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;/* this is an embedded lua interpreter,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;like if you opened the interpreter from the executable directly<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gathers input, crtl + Z exits<br />&nbsp;&nbsp;&nbsp;&nbsp; */<br />&nbsp;&nbsp;&nbsp;&nbsp;while&#40;fgets&#40;buff, sizeof&#40;buff&#41;, stdin&#41; != NULL&#41; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;error = luaL_loadbuffer&#40;L, buff, strlen&#40;buff&#41;, &#34;line&#34;&#41; ||<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lua_pcall&#40;L, 0, 0,0 &#41;;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&#40;error&#41;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf&#40;stderr, &#34;%s&#34;, lua_tostring&#40;L, -1&#41;&#41;;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lua_pop&#40;L, 1&#41;; /*pop error message from the stack*/<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;lua_close&#40;L&#41;; /*clean up*/<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;return 0;<br />}<br /><!--c2--></div><!--ec2--><br /><br />In the above code, C is the controller (application) and Lua is the library used. Without getting too far ahead of myself, here's a quick list of how to embed Lua in generally any language since the APIs are so similar:<br /><br />1. Ensure the Lua libraries are imported correctly<br />2. Open up a new Lua state, this is your handle to everything<br />3. Open any other libraries you may need<br />4. Perform your program's functions utilizing the API as needed<br />5. Clean up after your Lua instance <br /><br />Whenever interfacing occurs, there is an API to handle transitions. This is of particular importance to us because C and Lua are radically different: dynamic versus static typing, garbage collection vs. not, so on and so forth. We cannot simply pass values without some sort of interface. The <a href="http://www.lua.org/manual/5.1/" target="_blank">C API</a> handles these issues (scroll down on that page).<br /><br /><b><!--sizeo:2--><span style="font-size:10pt;line-height:100%"><!--/sizeo--><u>The Stack:</u><!--sizec--></span><!--/sizec--></b><br /><br />Lua uses an abstract stack to exchange values between the language it is embedded in and itself. Each slot in this stack can hold any Lua value (for a list, check the <a href="http://www.lua.org/manual/5.1/" target="_blank">reference manual</a> or a previous tutorial). Data exchange follows the following rough guidelines:<br /><br /><u>To retrieve a value <b>from</b> Lua:</u><br />   1. Call Lua<br />   2. Lua pushes the value onto the stack<br />   3. Retrieve value from stack in embedding language<br />   4. Use value<br /><br /><br /><u>To pass a value <b>to </b>Lua:</u><br />   1. Push value onto the stack from embedding language<br />   2. Call Lua<br />   3. Lua pops value from stack<br />   4. Use value<br /><br /><br />There are different functions for each data type, but the concept is the same regardless of the situation. The stack is managed by Lua so it knows which value the embedding language is using and will not garbage collect them. [This seems simple, but that fact is extremely important, if C was in charge of those values, Lua might inadvertently "eat" them since it isn't aware of their usage]. When Lua starts the stack has at least 20 free "slots" (which is defined as a constant <span class="inlinecode">LUA_MINSTACK</span> in lua.h), so it is important to keep tabs on your usage, which you can do with <a href="http://www.lua.org/manual/5.1/manual.html#lua_checkstack" target="_blank">this function</a>. Remember that indexes start at 1 in Lua, so the stack index starts at 1 as well. You can also access the stack with negative indexes from the view of the top, so -1 refers to the element at the top, so on and so forth. I find that it is helpful when you want to default to the top (like when printing error messages). <br /><br /><u>Some examples:</u><br /><br />In this code, we use C to get a global value from a Lua file in the local directory. The contents of the Lua file are as follows:<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--><br />xCursor = 5<br /><!--c2--></div><!--ec2--><br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--><br />#include &#60;cstdio&#62;<br />#include &#60;cstring&#62;<br /><br />#ifdef __cplusplus<br />extern &#34;C&#34; {<br />#endif<br />#include &#60;lua.h&#62;<br />#include &#60;lauxlib.h&#62;<br />#include &#60;lualib.h&#62;<br />#ifdef __cplusplus<br />}<br />#endif<br /><br />int main&#40;&#41;<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;lua_State *L = luaL_newstate&#40;&#41;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*open a new lua state*/<br />&nbsp;&nbsp;&nbsp;&nbsp;luaL_openlibs&#40;L&#41;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*open standard libraries*/<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;luaL_dofile&#40;L, &#34;test.lua&#34;&#41;;&nbsp;&nbsp;&nbsp;&nbsp;/* open up a lua file in the current directory*/<br />&nbsp;&nbsp;&nbsp;&nbsp;lua_getglobal&#40;L, &#34;xCursor&#34;&#41;; /* push a global onto the stack*/<br />&nbsp;&nbsp;&nbsp;&nbsp;printf&#40;&#34;%d&#092;n&#092;n&#34;, lua_tointeger&#40;L,lua_gettop&#40;L&#41;&#41;&#41;; /*retrieve and print*/<br />&nbsp;&nbsp;&nbsp;&nbsp;lua_close&#40;L&#41;; /*clean up*/<br />&nbsp;&nbsp;&nbsp;&nbsp;return 0;<br />}<br /><!--c2--></div><!--ec2--><br /><br />In this next example, we're going to call a function in Lua from C, the contents of the Lua file:<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--><br />function f&#40;x, y&#41;<br />&nbsp;&nbsp;&nbsp;&nbsp;return &#40;x^2*math.sin&#40;y&#41;&#41;/&#40;1-x&#41;<br />end<br /><!--c2--></div><!--ec2--><br /><br />We call the Lua function in C, we then pass two values and call <span class="inlinecode">lua_pcall</span> which takes the lua handle, the number of arguments, the number of results, and an error handling number (zero indicates that we should print the exact, original error message):<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--><br />#include &#60;cstdio&#62;<br />#include &#60;cstring&#62;<br /><br />#ifdef __cplusplus<br />extern &#34;C&#34; {<br />#endif<br />#include &#60;lua.h&#62;<br />#include &#60;lauxlib.h&#62;<br />#include &#60;lualib.h&#62;<br />#ifdef __cplusplus<br />}<br />#endif<br /><br />int main&#40;&#41;<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;lua_State *L = luaL_newstate&#40;&#41;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*open a new lua state*/<br />&nbsp;&nbsp;&nbsp;&nbsp;luaL_openlibs&#40;L&#41;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*open standard libraries*/<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;/*vars we'll pas to Lua*/<br />&nbsp;&nbsp;&nbsp;&nbsp;double x = 5, y = 6, result;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;luaL_dofile&#40;L, &#34;test.lua&#34;&#41;;&nbsp;&nbsp;&nbsp;&nbsp;/* open up a lua file in the current directory*/<br />&nbsp;&nbsp;&nbsp;&nbsp;lua_getglobal&#40;L, &#34;f&#34;&#41;; /* push a global onto the stack*/<br />&nbsp;&nbsp;&nbsp;&nbsp;lua_pushnumber&#40;L, x&#41;;<br />&nbsp;&nbsp;&nbsp;&nbsp;lua_pushnumber&#40;L, y&#41;;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;/*2 arguments, 1 result*/<br />&nbsp;&nbsp;&nbsp;&nbsp;if&#40;lua_pcall&#40;L, 2, 1, 0&#41; != 0&#41;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* if a problem arose, print the error from the top of stack &#40;-1&#41;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;luaL_error&#40;L, &#34;error running function 'f'&#58; %s&#34;, lua_tostring&#40;L, -1&#41;&#41;;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;/* it succeeded, get value off the stack */<br />&nbsp;&nbsp;&nbsp;&nbsp;result = lua_tonumber&#40;L, -1&#41;;<br />&nbsp;&nbsp;&nbsp;&nbsp;lua_pop&#40;L, 1&#41;; /*pop it*/<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;printf&#40;&#34;&#092;n&#092;nResult&#58; %f&#092;n&#092;n&#34;, result&#41;;<br />&nbsp;&nbsp;&nbsp;&nbsp;lua_close&#40;L&#41;; /*clean up*/<br />&nbsp;&nbsp;&nbsp;&nbsp;return 0;<br />}<br /><!--c2--></div><!--ec2--><br /><br /><br />Output:<br /><br /><!--quoteo--><div class='quotetop'>QUOTE</div><div class='quotemain'><!--quotec--><br />1.746347<br /><!--QuoteEnd--></div><!--QuoteEEnd--><br /><br />(If you wanted to verify on your calculator, make sure you're in Radian mode). <br /><br />To show how consistent the APIs are, here is an example in Java of what we just did with C:<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--><br />import org.keplerproject.luajava.LuaObject;<br />import org.keplerproject.luajava.LuaState;<br />import org.keplerproject.luajava.LuaStateFactory;<br /><br />public class LuaJavaTest {<br /><br />&nbsp;&nbsp;&nbsp;&nbsp; public static void main&#40;String&#91;&#93; args&#41;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LuaState L = LuaStateFactory.newLuaState&#40;&#41;; //create state<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;L.LdoFile&#40;&#34;test.lua&#34;&#41;; //open external file<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;L.getGlobal&#40;&#34;xCursor&#34;&#41;; //retrieve value<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println&#40;&#34;xCursor's value is&#58;&#34; + L.toInteger&#40;L.getTop&#40;&#41;&#41;&#41;; //pop it off and print<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><!--c2--></div><!--ec2--><br /><br />I found to be a bit more troublesome to set up Lua with Java, but after experimenting a bit I was able to get <a href="http://www.keplerproject.org/luajava/" target="_blank">LuaJava</a> up and running. Make sure you add it as an external library to your project. Netbeans allows this to be done rather easily, but feel free to use whatever you want. <br /><br />Hopefully you found this post helpful. There are many more things you can do when you embed Lua in another language, but the stack concept is consistent (not mention crucial) wherever your Lua endeavors take you. Happy Coding!]]></description>
		<pubDate>Fri, 20 Nov 2009 11:15:43 -0600</pubDate>
		<guid><![CDATA[http://www.dreamincode.net/forums/index.php?automodule=blog&blogid=324&showentry=1923]]></guid>
	</item>
	<item>
		<title>ElcriccirclE - Pascal(Delphi) to C++</title>
		<link><![CDATA[http://www.dreamincode.net/forums/index.php?automodule=blog&blogid=551&showentry=1922]]></link>
		<category></category>
		<description><![CDATA[<b><!--sizeo:3--><span style="font-size:12pt;line-height:100%"><!--/sizeo-->Dr. Detlef Meyer-Eltz - 1.0.0: Converter from Object-Pascal(Delphi) to C++<!--sizec--></span><!--/sizec--></b>  <a href="http://www.openpr.com/news/107333/Delphi2Cpp-1-0-0-Converter-from-Object-Pascal-Delphi-to-C.html" target="_blank">http://www.openpr.com/news/107333/Delphi2C...elphi-to-C.html</a>]]></description>
		<pubDate>Thu, 19 Nov 2009 10:09:24 -0600</pubDate>
		<guid><![CDATA[http://www.dreamincode.net/forums/index.php?automodule=blog&blogid=551&showentry=1922]]></guid>
	</item>
	<item>
		<title><![CDATA[crzyone9584's Blog - Venatio Project]]></title>
		<link><![CDATA[http://www.dreamincode.net/forums/index.php?automodule=blog&blogid=799&showentry=1920]]></link>
		<category>Venatio Project</category>
		<description><![CDATA[Hello guys,<br /><br />This is my first blog. This blog will be about my projects. The main project that I am working on is called Venatio Project. This is a mix of IGN and Gamespy Arcade.<br /><br />Venatio is a Greek work that means Games.<br /><br />Here is the list of features I plan for it.<br /><br />Here is a list of features released to the public planned for the Venatio Project.<br /><br /><!--quoteo--><div class='quotetop'>QUOTE</div><div class='quotemain'><!--quotec--><!--coloro:#FF0000--><span style="color:#FF0000"><!--/coloro-->In Progress<!--colorc--></span><!--/colorc--><br /><!--coloro:#000099--><span style="color:#000099"><!--/coloro-->Done<!--colorc--></span><!--/colorc--><br /><br />Beta (Test)<br />-Game Name and Game Info<br />- Password Encryption<br />-Music<br />-Logo/ 3-D Character<br />-Connect to the Site<br />-Connect to the Database<br />-Login<br />-User Registration<br />-User Levels<br />Admin Options<br />-Add User<br />-Edit User<br /><br /><br />Version 0.1 First Public release<br />-All features from Beta<br />-Ability to add/update/edit Database through program. (admins, Moderators only)<br />-Useer Game score/Like IGN<br />-Boxart Showed<br />- User comments for game posted on program.<br /><br />Version 0.2 Second Public Release<br />-links to guides/FAQ/Walkthroughs<br />-users able to submit guides/FAQ/Walkthoughs<br />- WIshlist, Collection, Now Playing, Played Games, Favorite game Catorgories<br /><br />Version 0.3<br />-Game News, Reveiws, and Previews<br />-User Submitted Reveiws<br /><br />Version 0.4<br />-List of Homebrew Games for Conoles (like xbox 360 arcade games)<br />-Downloadable Game Saves<!--QuoteEnd--></div><!--QuoteEEnd--><br /><br />This will take me a long time to complete. But For most of it, its just learning vb.net along with asp.net written in vb.net. I'm the only one who is working on this. The beta is not in the near future. I will update this blog about once a month with new information about how the project is going. <br /><br />On a side note I also have other side projects going to help me with my main project. The side projects will eventually be put into the Venatio Project.]]></description>
		<pubDate>Wed, 18 Nov 2009 14:45:55 -0600</pubDate>
		<guid><![CDATA[http://www.dreamincode.net/forums/index.php?automodule=blog&blogid=799&showentry=1920]]></guid>
	</item>
	<item>
		<title>ElcriccirclE - Concurrency Runtime - Asynchronous Agents Library </title>
		<link><![CDATA[http://www.dreamincode.net/forums/index.php?automodule=blog&blogid=551&showentry=1919]]></link>
		<category></category>
		<description><![CDATA[<b><!--sizeo:3--><span style="font-size:12pt;line-height:100%"><!--/sizeo-->Phil Pennington - C++ Concurrency Runtime - Asynchronous Agents Library<!--sizec--></span><!--/sizec--></b>  <a href="http://channel9.msdn.com/posts/philpenn/The-C-Concurrency-Runtime-Asynchronous-Agents-Library/" target="_blank">http://channel9.msdn.com/posts/philpenn/Th...Agents-Library/</a>]]></description>
		<pubDate>Wed, 18 Nov 2009 07:07:30 -0600</pubDate>
		<guid><![CDATA[http://www.dreamincode.net/forums/index.php?automodule=blog&blogid=551&showentry=1919]]></guid>
	</item>
	<item>
		<title>ElcriccirclE - Metaprogramming in D</title>
		<link><![CDATA[http://www.dreamincode.net/forums/index.php?automodule=blog&blogid=551&showentry=1918]]></link>
		<category></category>
		<description><![CDATA[<b><!--sizeo:3--><span style="font-size:12pt;line-height:100%"><!--/sizeo-->Walter Bright: Metaprogramming in D<!--sizec--></span><!--/sizec--></b>  <a href="http://www.vimeo.com/4333802" target="_blank">http://www.vimeo.com/4333802</a>]]></description>
		<pubDate>Tue, 17 Nov 2009 17:13:54 -0600</pubDate>
		<guid><![CDATA[http://www.dreamincode.net/forums/index.php?automodule=blog&blogid=551&showentry=1918]]></guid>
	</item>
	<item>
		<title>ElcriccirclE - Next C++ Standard</title>
		<link><![CDATA[http://www.dreamincode.net/forums/index.php?automodule=blog&blogid=551&showentry=1917]]></link>
		<category></category>
		<description><![CDATA[<b><!--sizeo:3--><span style="font-size:12pt;line-height:100%"><!--/sizeo-->Lawrence Crowl - New Features in the Next C++ Standard<!--sizec--></span><!--/sizec--></b>  <a href="http://www.youtube.com/watch?v=ZAG5txfYnW4" target="_blank">http://www.youtube.com/watch?v=ZAG5txfYnW4</a>]]></description>
		<pubDate>Tue, 17 Nov 2009 05:50:11 -0600</pubDate>
		<guid><![CDATA[http://www.dreamincode.net/forums/index.php?automodule=blog&blogid=551&showentry=1917]]></guid>
	</item>
	<item>
		<title>CodeTalk - Online IDEs – perspectives?</title>
		<link><![CDATA[http://www.dreamincode.net/forums/index.php?automodule=blog&blogid=334&showentry=1916]]></link>
		<category>General</category>
		<description><![CDATA[As of recently on the development horizon appeared several new tendencies, one of them being the creation of the so-called online IDEs. Online IDEs are development environments that can be accessed from the web browser, without the need to install specific client software. At this time those few that are present on the arena have very limited functionality, although even at this time offer great opportunities for aspiring and professional developers.<br /><br />So, why do I think that there is a niche for online IDEs and those will find their users?<br /><br /><b>1. Platform-independent</b><br />Online run just as a regular web page – from a web browser. The most we could probably expect is the need to install a third-party plug-in for the web browser (like ActiveX or Silverlight) to display the contents. And that’s it. All the code compilation, linking and debugging can be made in the cloud (on a remote machine). This means, that the platform-dependent layer is slowly becoming transparent, allowing developers to create their software on a platform, where initially the language is not supported. Another example would be that the client machine is a bit “slow” to allow the developer to work with a regular IDE, and here is where its online analog takes a strong position, making the development possible at different levels. The developer won’t have to worry about hardware anymore.<br /><b><br />2. Accessibility</b><br />The online IDE can be accessed from any place in the world, just a few clicks away. So, you won’t have to carry that 12-project solution with you to every single trip to a different town, city or country. This also means that you don’t have to worry about storage.<br /><br /><b>3. Easier collaboration</b><br />Major projects are made in teams, and usually there is additional software needed to setup a team development environment. When all the code is stored on a remote server and the development environment is accessible to everyone, it is very easy to collaborate in the framework of a specific project. All you will ever have to worry about is the server configuration for teamwork.<br /><br /><b>4. Upgradeability</b><br />This mostly refers to the developers of online IDEs, but I also decided including it here. It is much easier to upgrade/patch a product on a single machine rather than distributing a service pack to thousands of clients. People want to work with stable, bug-free software on any level, and a single upgrade in a single location could be the first step. Also, whenever a new SDK or framework appears, the developers most likely start working on upgrading their machines, sometimes more painful because of compatibility issues, sometimes this is a less-painful process. However, with a single instance of the development platform running remotely it will be much easier to add the needed features without the fear of messing up the whole development environment.<br /><br />Although online IDEs have several benefits, there are also drawbacks. One of them being the code security. Not many developers are willing to let their code “live” on a remote machine that is not controlled by them. Mainly because of the possibility of data loss, this already happened before in several cloud services. And in most cases the vendor of the service takes no liability of the consequences. But there is one more thing – any piece of code that is created in the online environment becomes visible to the developer of the IDE and the team managing it. Therefore, working with private code or code that is not intended to be used in public/commercial applications is unsecure.<br /><br />At this time online IDEs run only in several place, however, I would expect that soon there will appear commercial and free distribution for corporate and third-party use. No matter how this turns out, but it is definitely interesting to watch this field develop and grow.<br />]]></description>
		<pubDate>Mon, 16 Nov 2009 15:38:32 -0600</pubDate>
		<guid><![CDATA[http://www.dreamincode.net/forums/index.php?automodule=blog&blogid=334&showentry=1916]]></guid>
	</item>
	<item>
		<title>Double Time and a Half - O( 2n(m^n) + n) !?!?!?!?!</title>
		<link><![CDATA[http://www.dreamincode.net/forums/index.php?automodule=blog&blogid=432&showentry=1915]]></link>
		<category></category>
		<description><![CDATA[If people ask me how I spent the last few hours of my teenager-hood, I can proudly state that I was coding...<br /><br />Was it something trivial? Yes.<br />Does it have any real-world importantce? No.<br />Could you have spent your time better? Quite possibly<br /><br />But in my opinion, A good programmer is expected to go off on tangents and code something just because. A better programmer codes something just for the hell of it and then works out how he can make the new code more efficient.<br /><br />You can imagine my horror when I worked out the big O notation of the program that I wrote to be able to calculate all the possible permutations of any number of any sided die, was to be in O( 2n(m^n) + n)... And that is just the generation of the permutations, I haven't even started on processing the data in any way.<br /><br />So as of my 20th Birthday I'm setting out to write better code:<br />	- More efficient<br />	- Less memory wastage<br />	- And so on<br /><br />What better way to make sure I write better code? Write a code-blog to make sure I'm always writing something and then make that 'something' not terrible!<br /><br />	Happy Coding!<br />	<br />	DoubleFIssion<br />]]></description>
		<pubDate>Mon, 16 Nov 2009 07:05:56 -0600</pubDate>
		<guid><![CDATA[http://www.dreamincode.net/forums/index.php?automodule=blog&blogid=432&showentry=1915]]></guid>
	</item>
	<item>
		<title>Stuck in an Infiniteloop - ATI TV WONDER 600: A Tale of Woe</title>
		<link><![CDATA[http://www.dreamincode.net/forums/index.php?automodule=blog&blogid=324&showentry=1914]]></link>
		<category></category>
		<description><![CDATA[About a week and a half ago Woot was having a nice sale on <a href="http://www.newegg.com/Product/Product.aspx?Item=N82E16815306011" target="_blank">this product</a> for <a href="http://www.woot.com/Blog/ViewEntry.aspx?Id=9854" target="_blank">20 bucks</a>. It was one of the purchases that was worth a 20 dollar mistake, just for the fun of playing with it. <br /><br /><img src="http://ati.amd.com/products/tvwonder600/USB/images/tv-wonder600_usb_sm.jpg" border="0" alt="IPB Image" /><br /><br /><u><b>Some backstory:</b></u><br />For the longest time, I have had a disdain for ATI products. I suppose it stemmed from the ATI card in my laptop which constantly had some issue going on (mainly driver related). I was beginning to get relaxed on my ATI stance (5870 looks awesome), but today was a total goat-rope. <br /><br /><br /><b><br /><u>Back to today:</u></b><br />The instructions looked like they were literally translated out of order:<br /><br />Install software. <br />When prompted, install hardware.<br />But you don't have to install the software first, Windows will autodetect. <br />Be sure you install the software first. <br />Open software and run it.<br />Now install hardware, Windows will autodetect.<br /><br />WHAT? (By the way, the autodetect part did not work)<br /><br />Some searching on the internet revealed that you have to download the driver separately from the stuff included on the install CD and THEN use the CD. Perhaps the directions should have mentioned that. The install (after rebooting your computer) throws some weird, obscure icon on your desktop. This icon is so weird and so poorly named it just looks like spyware, but it's not. Turns out it is the driver for the remote control that came with the tuner. Again, not explained in the install guide. <br /><br />After all this ado I finally got the thing functional, but now I only have 8 channels:<br /><br />1. local ABC<br />2. some local Spanish channel <br />3. a 24/7 weather channel<br />4. a televangelist channel<br />5. some local channel, think a low budget sesame street channel<br />6. same as number 5<br />7. PBS<br />8. the Pentagon channel<br /><br />SWEET! <b>(not sweet, I am pissed)</b> I uploaded an EPG (electronic programming guide) and based on the local free over the air digital channels listed, I should be getting as least 4 times as many channels including local fox, nbc, and cbs ones. My current setup defeats the purpose of buying this thing. I'll play with it some more, hopefully I can get a few more of the local affiliates. <br /><br />The software (Catalyst Media Center) was up and running it actually pretty good. I hear Windows Media Center is better, but since i'm still on XP Pro, I don't have access to it. Recording TV was easy, I haven't tried automating it yet. <br /><br /><br /><br />For what I do have, the picture quality is excellent, HD FTW.<br /><br /><br /><br /><br /><b><u>UPDATE:</u></b><br /><br />It turns out that the lack of channels is due to the low quality of the included antenna. I think I'll hit up Radioshack later today and see what the next tier options are.]]></description>
		<pubDate>Fri, 13 Nov 2009 19:23:35 -0600</pubDate>
		<guid><![CDATA[http://www.dreamincode.net/forums/index.php?automodule=blog&blogid=324&showentry=1914]]></guid>
	</item>
</channel>
</rss>