MSYS is the way you want to go about building Lua from a makefile. I think it's been hacked to work with Visual C++ as well, though?
[QUOTE=Wyzard;36719769]For a multi-dimensional array, all the dimensions except the last have to be constants. But you can use a single-dimensional array, and convert the x/y pair into an array index yourself, and that way you can choose the size of the array at runtime.[/QUOTE]
Sorry I've been asking so many questions, but i'm not really sure what you mean by that.
2D arrays in your code are really stored as 1D arrays in RAM, because memory addresses are 1D. When you refer to an element using the 2D coordinates, the compiler generates machine code that does some arithmetic to figure out the corresponding place in the underlying 1D array. To do that, the compiler has to know the array's width, which is why it has to be a constant. But you can implement the same arithmetic yourself even if the width is stored in a variable.
For example, you can get the effect of a 10x10 2D array by using a 100-element 1D array. The top row is elements 0 through 9, the second row is elements 10 through 19, and so on until the bottom row, which is elements 90 through 99. To calculate the 1D array index for a pair of X and Y values, you do (y*width+x). Row 3 column 4, for example, translates to index (3*10+4)=34 in the 1D array.
[QUOTE=Wyzard;36745144]2D arrays in your code are really stored as 1D arrays in RAM, because memory addresses are 1D. When you refer to an element using the 2D coordinates, the compiler generates machine code that does some arithmetic to figure out the corresponding place in the underlying 1D array. To do that, the compiler has to know the array's width, which is why it has to be a constant. But you can implement the same arithmetic yourself even if the width is stored in a variable.
For example, you can get the effect of a 10x10 2D array by using a 100-element 1D array. The top row is elements 0 through 9, the second row is elements 10 through 19, and so on until the bottom row, which is elements 90 through 99. To calculate the 1D array index for a pair of X and Y values, you do (y*width+x). Row 3 column 4, for example, translates to index (3*10+4)=34 in the 1D array.[/QUOTE]
Ok, I didn't know that about the 1d storage in memory... i'll try this out definitely.
But what you are saying is basically story it as a 1d array, then make a little function that makes it behave like a 2d array, right?
And thanks so much for all the help, I really have learned quite a bit in a very short amount of time due to the help you guys are giving me. I really appreciate how nice people are in the coding sub form.
Is it possible to give a single shader to many entities, but allow each entity to change their own shader?
The way I have it right now, each entity changes the main shader instead of its own.
[QUOTE=Jimmylaw;36750690]Is it possible to give a single shader to many entities, but allow each entity to change their own shader?
The way I have it right now, each entity changes the main shader instead of its own.[/QUOTE]
If by change their own shader you mean change its uniform or attribute variables then yea its possible
[QUOTE=Richy19;36751152]If by change their own shader you mean change its uniform or attribute variables then yea its possible[/QUOTE]
The only attribute is the world position of the shader that needs to be changed.
[cpp]
ID3DXEffect *g_pEffect;
D3DXCreateEffectFromFile( device, "Ambient.fx", NULL, NULL, NULL, NULL, &g_pEffect, NULL );
for (int z = 1; z <= 10; z++)
{
for (int x = 1; x <= 10; x++)
{
shaderObject* temp = new shaderObject(device,this);
temp->position = D3DXVECTOR3(-x * 5,0, -z * 5);
temp->setShader(g_pEffect);
_gameObjectManager.Add(temp);
}
}
[/cpp]
The shader is created and then given to each entity.
[cpp]
void shaderObject::setShader(ID3DXEffect* _shader)
{
g_pEffect = _shader;
}
[/cpp]
Instead of drawing 100 entites, it draws 100 entites at one position. I think its doing this because its drawing them all at the position of the last entity in the array. So instead of each entity changing its own shader, its changing a global one that they all appear to be using.
I hope this makes sense.
I feel like an idiot for having to ask this but anyway.
I'm using xna and I'm trying to create a right click drag motion to navigate a 2D map.
How do I store the X and Y variables for the mouse position and get it to continue through the update loop without updating the variables each time it goes through the loop until the right mouse button has been released and pressed again?
So pretty much this.
[csharp]
if (MouseState.RightButton == ButtonState.Pressed)
{
x = MouseState.X;
y = MouseState.Y;
}
[/csharp]
But x and y aren't updated each time just when the button is released and pressed again.
[QUOTE=reevezy67;36751977]I feel like an idiot for having to ask this but anyway.
I'm using xna and I'm trying to create a right click drag motion to navigate a 2D map.
How do I store the X and Y variables for the mouse position and get it to continue through the update loop without updating the variables each time it goes through the loop until the right mouse button has been released and pressed again?[/QUOTE]
Keep track of current and old mouse states, something like:
[code]
MouseState ms_old, ms_cur;
...
//in per-frame update:
ms_old = ms_cur;
ms_cur = Mouse.GetState();
//check key presses here
[/code]
then you can check to see if the current state has the button pressed and the old one doesn't (so it's just been clicked):
[code]
if(ms_old.RightButton == ButtonState.Released && ms_cur.RightButton == ButtonState.Pressed)
{
dragging = true;
dragStartPos.X = ms_cur.X;
dragStartPos.Y = ms_cur.Y;
}
[/code]
[editline]13th July 2012[/editline]
then when they release (if old state pressed but current state released), you can do stuff too
[editline]13th July 2012[/editline]
Also a cool thing you could try is to:
a) when they right click, hide the mouse
b) every frame while it is still held, snap the mouse position to window center and measure the offset (from the center)
c) add that offset to the camera position
c) when they release it, show the mouse again
this lets you move the camera forever without using raw mouse input, or letting the cursor outside your window.
Well that was easy thanks, already implemented.
I'll try those suggestions too.
Im trying to implement screenshots with this code:
[cpp]
bool Program::screenshot(const std::string &filename, int x, int y, int width, int height)
{
unsigned char *pixel_data;
int i, j;
/* error checks */
if( (width < 1) || (height < 1) )
{
return false;
}
if( (x < 0) || (y < 0) )
{
return false;
}
/* Get the data from OpenGL */
pixel_data = (unsigned char*)malloc( 3*width*height );
glReadPixels (x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, pixel_data);
/* invert the image */
for( j = 0; j*2 < height; ++j )
{
int index1 = j * width * 3;
int index2 = (height - 1 - j) * width * 3;
for( i = width * 3; i > 0; --i )
{
unsigned char temp = pixel_data[index1];
pixel_data[index1] = pixel_data[index2];
pixel_data[index2] = temp;
++index1;
++index2;
}
}
/* save the image */
sf::Image im;
im.create(width, height, pixel_data);
im.saveToFile(filename);
/* And free the memory */
free( (void*) pixel_data );
return true;
}
[/cpp]
This is what it should look like:
[IMG]http://i.imgur.com/UgKR2.png[/IMG]
However this is what it produces:
[IMG]http://i.imgur.com/i1CET.png[/IMG]
Does it work (upside-down) if you take the inverting code block out?
SFML expects 32-bit color (so including alpha), your pixel array is invalid.
[QUOTE=NovembrDobby;36753180]Does it work (upside-down) if you take the inverting code block out?[/QUOTE]
[QUOTE=Overv;36753396]SFML expects 32-bit color (so including alpha), your pixel array is invalid.[/QUOTE]
Thanks to you both :D, the flip code was making it repeat and the 3channel image was making it not draw correctly. changed it to this and it works fine :D
[cpp]
bool Program::screenshot(const std::string &filename, int x, int y, int width, int height)
{
unsigned char *pixel_data;
int i, j;
/* error checks */
if( (width < 1) || (height < 1) )
{
return false;
}
if( (x < 0) || (y < 0) )
{
return false;
}
/* Get the data from OpenGL */
pixel_data = (unsigned char*)malloc( 4*width*height );
glReadPixels (x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixel_data);
/* save the image */
sf::Image im;
im.create(width, height, pixel_data);
im.flipVertically();
im.saveToFile(filename);
/* And free the memory */
free( (void*) pixel_data );
return true;
}[/cpp]
Is there a better way to do it? as in not using malloc/free?
[QUOTE=Richy19;36753561]Is there a better way to do it? as in not using malloc/free?[/QUOTE]
You can't avoid using malloc, but you could keep a static data array that is free'd/malloc'd only when the resolution is changed. That would get rid of the allocation overhead when you screenshot.
[QUOTE=Richy19;36753561]Thanks to you both :D, the flip code was making it repeat and the 3channel image was making it not draw correctly. changed it to this and it works fine :D
[cpp]Is there a better way to do it? as in not using malloc/free?[/QUOTE]
For one, why aren't you using new and delete? Ignoring that, you can skip the double allocation altogether:
[cpp]bool Program::screenshot( const std::string& filename, int x, int y, int width, int height )
{
if( width < 1 || height < 1 || x < 0 || y < 0 )
return false;
sf::Image im( width, height );
glReadPixels( x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, const_cast<sf::Uint8*>( im.GetPixelsPtr() ) );
im.flipVertically();
im.saveToFile( filename );
return true;
}[/cpp]
Is there an easy way to clamp a rotation matrix to an angle? The problem is I don't want to store the yaw and pitch angle for a camera but I need them to clamp them between -90 to 90 degrees.
The reason I don't want to store them is because it's all well and fine accumulating the angles from input but if I change the camera rotation directly via a LookAt or something, the yaw and pitch angles arent in sync anymore with the camera rotation.
It seems most people just extract the pitch back out of the rotation matrix after the LookAt, bleh.
[QUOTE=Mr. Smartass;36699854]What'd be the best method of storing bullets? I originally figured that I could store them in a list, but then I noticed that there was surely a better and more efficient way to do it.[/QUOTE]
tbh i'm probably a terrible c++ programmer in terms of technical detail and efficiency, but as far as i'm aware vectors would probably be a good choice. according to the cplusplus.com page, they're "generally the most efficient in time for accessing elements and to add or remove elements from the end of the sequence", so it seems like it'd work well for you. vectors are what i use to store nodes in my scene graph.
new problem, trying to have a go at lua integration again with this:
[cpp]
#include <iostream>
#include <string>
#include <lua.hpp>
int main()
{
lua_State* luaVM = lua_open();
if (luaVM == NULL)
{
std::cout << "Error Initializing lua" << std::endl;
return -1;
}
luaL_openlibs(luaVM);
std::string s = "print(\"Hi\");";
luaL_dostring(luaVM, s.c_str());
lua_close( luaVM );
return 0;
}
[/cpp]
But i get this:
[QUOTE]/usr/local/lib/liblua.a(loadlib.o)||In function `ll_loadfunc':|
loadlib.c|| undefined reference to `dlsym'|
loadlib.c|| undefined reference to `dlopen'|
loadlib.c|| undefined reference to `dlerror'|
loadlib.c|| undefined reference to `dlerror'|
/usr/local/lib/liblua.a(loadlib.o)||In function `gctm':|
loadlib.c|| undefined reference to `dlclose'|
||=== Build finished: 5 errors, 0 warnings (0 minutes, 0 seconds) ===|
[/QUOTE]
[QUOTE=Richy19;36755883]new problem, trying to have a go at lua integration again with this:
[cpp]
#include <iostream>
#include <string>
#include <lua.hpp>
int main()
{
lua_State* luaVM = lua_open();
if (luaVM == NULL)
{
std::cout << "Error Initializing lua" << std::endl;
return -1;
}
luaL_openlibs(luaVM);
std::string s = "print(\"Hi\");";
luaL_dostring(luaVM, s.c_str());
lua_close( luaVM );
return 0;
}
[/cpp]
But i get this:[/QUOTE]
try this
[cpp]
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
[/cpp]
[QUOTE=Kopimi;36755930]try this
[cpp]
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
[/cpp][/QUOTE]
That is the contents of lua.hpp
oh sheesh
I keep readin lauxlib.h as lau-x-lib
Question, is there a way to define a type in C++ that would store [i]exactly[/i] N bytes?
Also, if I do "typdef unsigned char dword[4]", can I use it as if it were an unsigned int, and not an array?
I'm trying to write this thing in python that checks if z is within x and y, all of which are determined by raw_input.
[code]int(x = raw_input('x:\n'))
int(y = raw_input('y:\n'))
int(z = raw_input('z:\n'))
if z < y:
print 'invalid.'
elif z > y:
print 'invalid.'
else:
print 'valid.'
[/code]
But I always get this:
Traceback (most recent call last):
File "C:/Python27/asdfa", line 2, in <module>
int(y = raw_input('y:\n'))
TypeError: 'y' is an invalid keyword argument for this function
I get the error regardless of what variable i use in place of y. I'm also wondering if i'm using elif properly.
Edit: I figured it out, I had to do y = int(raw_input) instead of int(y = raw_input).
[QUOTE=Nikita;36757996]Question, is there a way to define a type in C++ that would store [i]exactly[/i] N bytes?
Also, if I do "typdef unsigned char dword[4]", can I use it as if it were an unsigned int, and not an array?[/QUOTE]
For the first one, doesn't a char array of exactly n bytes work?
For the second one, *((unsigned int *)dword)
[editline]14th July 2012[/editline]
Except, of course, you don't want to use a C cast. I can't remember what the correct one here is, though, reinterpret or static?
I mean that I want a type that stores exactly as many bytes as I need, and one that I could use as if it where a whole thing (char, int, double), not as a pointer and not as array.
Arrays and templated classes?
[QUOTE=Nikita;36758296]I mean that I want a type that stores exactly as many bytes as I need, and one that I could use as if it where a whole thing (char, int, double), not as a pointer and not as array.[/QUOTE]
You probably want one of the types in <cstdint>:
[cpp]int8_t
uint8_t
int16_t
uint16_t
int32_t
uint32_t
int64_t
uint64_t[/cpp]
These are guaranteed to have the specified amount of bits.
Sorry, you need to Log In to post a reply to this thread.