When I compile I get thrown a bad_alloc. I think it is either coming from my read function not remembering the char array. can someone help?
[cpp]
char *ReadFromFile(char *Filepath)
{
int length;
char * Shader;
ifstream is;
is.open (Filepath);
if (!is) {
cout << "\nERROR: Unable to open file";
}
is.seekg (0, ios::end);
length = is.tellg();
is.seekg (0, ios::beg);
Shader = new char [length];
is.read (Shader,length);
is.close();
return Shader;
delete[] Shader;
}
static GLuint ShaderCompile(GLenum type, char *filePath)
{
char *source;
GLuint shader;
GLint length, result;
source = ReadFromFile(filePath);
if(!source)
return 0;
shader = glCreateShader(type);
length = sizeof(source);
glShaderSource(shader, 1, (const char **)&source, &length);
glCompileShader(shader);
free(source);
glGetShaderiv(shader, GL_COMPILE_STATUS, &result);
if(result == GL_FALSE) {
glDeleteShader(shader);
return 0;
}
return shader;
}
static GLuint ProgramGen(GLuint vertex_shader, GLuint fragment_shader)
{
GLint program_ok;
GLuint program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &program_ok);
return program;
}
void SetShaderandProgram()
{
ProgramGen(ShaderCompile(GL_VERTEX_SHADER, ReadFromFile("C:/Users/SystemID/Programming/GLUT/vertex.vert")),
ShaderCompile(GL_FRAGMENT_SHADER, ReadFromFile("C:/Users/SystemID/Programming/GLUT/fragment.frag")));
}
[/cpp]
Also when I add 1 to the length of the char array then it doesn't load the file and the program crashes.
[QUOTE=Mozartkugeln;38156042]To get a number from a [I]JTextField[/I], I suggest adding an [I]ActionListener[/I] to it, just like you would to a [I]JButton[/I]. Keep in mind that this requires pressing Enter.
If you wish to convert as soon as the user stops typing, a clean way of doing this would be adding a [I]FocusListener[/I] to the text field and implementing the [I]focusLost[/I] method to call the conversion method. This will convert the units when the text field component loses focus (e.g. when you click on something else).
As far as the combo boxes go, they also work with [I]ActionListeners[/I] — whenever a user selects an option, the listener is triggered. This, of course, means that you can do something along the lines of this:
[/QUOTE]
Got the JComboBox updater working, but now it's not executing the method [i]f2c[/i]. Enumerators are out of the question due to class assignment. Been working on it since last week and it's due tomorrow sometime and all that.
[cpp]package tempconv;
import javax.swing.*;
import java.awt.event.*;
public class Tempconv {
// FAHRENHEIT
static double f2c(double temp) { // Fahrenheit/Imperial to Celsius/Metric (°F-°C)
return (5/9)*(temp-32); }
static double f2k(double temp) { // Fahrenheit/Imperial to Kelvin (°F-°K)
return (temp + 459.67) * (5/9); }
// CENTIGRADES
static double c2f(double temp) { // Celsius/Metric to Fahrenheit/Imperial (°C-°F)
return temp * (9/5) + 32; }
static double c2k(double temp) { // Celsius/Metric to Kelvin (°C-°K)
return temp+273.15; }
// KELVIN
static double k2f(double temp) { // Kelvin to Fahrenheit/Imperial (°K-°F)
return temp * (9/5) - 459.67; }
static double k2c(double temp) { // Kelvin to Celsius/Metric (°K-°C)
return temp - 273.15; }
static void check(double temp, String from, String to) {
final ui frame = new ui();
if (from.equals("°F") && to.equals("°C")) {
//double buff = Double.parseDouble(frame.txt_Temp.getText());
frame.l_result.setText(Double.toString(f2c(temp))); }
}
public static void main(String[] args) {
// Let's set up some frame properties here, first.
final ui frame = new ui();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(275, 85);
frame.setResizable(false);
frame.cb_fromunit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
}
});
frame.cb_tounit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
// Set your unit field or w/e you have to comboBoxUnits.getSelectedItem(), then call the conversion method.
// DO WHATEVER on this line, preferrably run temp. conversion
//double temp = Double.parseDouble(frame.txt_Temp.getText());
//frame.l_result.setText(Double.toString(f2c(temp)));
Object from = frame.cb_fromunit.getSelectedItem();
Object to = frame.cb_tounit.getSelectedItem();
check(Double.parseDouble(frame.txt_Temp.getText()), from.toString(), from.toString());
}
});
}}[/cpp]
[QUOTE=chaoselite;38158060]When I compile I get thrown a bad_alloc. I think it is either coming from my read function not remembering the char array. can someone help?
Also when I add 1 to the length of the char array then it doesn't load the file and the program crashes.[/QUOTE]
You're using free() on something that's been new[]'d. You have to use delete[].
Also, since you're using C++ and not C, stop mucking about with the heap unnecessarily.
[url=http://stackoverflow.com/a/2602060/823542]How to read a whole ASCII file into a C++ std::string[/url]
I have a rather simple (I think) question. I'm trying to understand this code, I've got it all down except the very end "map" function.
The original code is here: [url]http://grathio.com/2009/11/secret_knock_detecting_door_lock/[/url]
Here's the part I don't understand. How does it "normalize it" and just get the tempo rather than the timing?
[CODE] for (i=0;i<maximumKnocks;i++){ // Normalize the times
knockReadings[i]= map(knockReadings[i],0, maxKnockInterval, 0, 100);
timeDiff = abs(knockReadings[i]-secretCode[i]);
if (timeDiff > rejectValue){ // Individual value too far out of whack
return false;
}
totaltimeDifferences += timeDiff;
}[/CODE]
Thanks!
[QUOTE=Stonecycle;38159925][CODE]
@Override
public void actionPerformed(ActionEvent evt) {
// Set your unit field or w/e you have to comboBoxUnits.getSelectedItem(), then call the conversion method.
// DO WHATEVER on this line, preferrably run temp. conversion
//double temp = Double.parseDouble(frame.txt_Temp.getText());
//frame.l_result.setText(Double.toString(f2c(temp)));
Object from = frame.cb_fromunit.getSelectedItem();
Object to = frame.cb_tounit.getSelectedItem();
check(Double.parseDouble(frame.txt_Temp.getText()), from.toString(), [B][U]from.toString()[/U][/B]);
}
[/CODE][/QUOTE]
You're passing in the same unit twice, i.e. you're converting from one unit to that exact same unit.
I suggest making [I]temp[/I], [I]from[/I] and [I]to[/I] private fields so you don't have to constantly pass them into the method as arguments; furthermore, your conversion methods and your [I]check()[/I] method should be private as well since you wouldn't need them outside that particular class.
[QUOTE=Mozartkugeln;38164836]You're passing in the same unit twice, i.e. you're converting from one unit to that exact same unit.
I suggest making [I]temp[/I], [I]from[/I] and [I]to[/I] private fields so you don't have to constantly pass them into the method as arguments; furthermore, your conversion methods and your [I]check()[/I] method should be private as well since you wouldn't need them outside that particular class.[/QUOTE]
Fixed that stuff after asking that time. Next problem is that the conversions are outputting as 0.0, but it identifies when to go negative. Testing with f2c first.
[cpp]static double f2c(double temp) { // Fahrenheit/Imperial to Celsius/Metric (°F-°C)
return ((temp-32) * (5/9)); }[/cpp]
[QUOTE=Topgamer7;38150168]I think you just probably need to surround your matches with [].
Edit: Dear got that was a terrible sentence.[/QUOTE]
Oh jesus how did I forget that :downs:
Thanks.
Still have no idea how to get the " in the regex though, since I can't escape that
Here's an update of what I've got now
[php]Dim specialRegex As New Regex("[!%&'*,-./" + Regex.Escape("+*#()$]"))[/php]
[QUOTE=Stonecycle;38165719]
[CODE]static double f2c(double temp) { // Fahrenheit/Imperial to Celsius/Metric (°F-°C)
return ((temp-32) * ([B][U]5/9[/B][/U])); }[/CODE][/QUOTE]
5 and 9 are ints, so the / operator makes the quotient an int as well. Replace "5/9" with "5.0/9.0" and you should be good.
[editline]24th October 2012[/editline]
[QUOTE=Bazkip;38166398]Still have no idea how to get the " in the regex though, since I can't escape that [/QUOTE]
Use two quotation marks to escape it. For example, "This is a ""string"" literal" should come out as:
[CODE]This is a "string" literal[/CODE]
[QUOTE=Bazkip;38166398]Oh jesus how did I forget that :downs:
Thanks.
Still have no idea how to get the " in the regex though, since I can't escape that
Here's an update of what I've got now
[php]Dim specialRegex As New Regex("[!%&'*,-./" + Regex.Escape("+*#()$]"))[/php][/QUOTE]
Typically in regex you use back slash to escape characters. so you would put something like "[!%\"]" for your regex. I don't think you need the Regex.Escape part. But I'm not that great with VB
Thank you guys, nearly done (and understanding it a little more), but there's one flaw left: the JLabel l_result is not updating within the check() method, so I placed a test l_result.setText() after. I say it's something wrong within check().
[cpp] private static void check(double temp, int from, int to) {
formulae f = new formulae();
final ui frame = new ui();
double save;
DecimalFormat tuPoint = new DecimalFormat("0.00");
//int fromunit = frame.cb_fromunit.getSelectedIndex();
//int tounit = frame.cb_tounit.getSelectedIndex();
// 0 for F, 1 for C, 2 for K
if (from == 0 && to == 0) {
System.out.printf("Fahrenheit stays at %.2f°F\n", temp);
frame.l_result.setText(Double.toString(temp));}
else if (from == 0 && to == 1) {
System.out.printf("Fahrenheit to Celsius, %.2f°C\n", f.f2c(temp));
save = f.f2c(temp); tuPoint.format(save); temp = save;
frame.l_result.setText(Double.toString(f.f2c(temp)));}
else if (from == 0 && to == 2) {
System.out.printf("Fahrenheit to Kelvin, %.2f°K\n", f.f2k(temp));
frame.l_result.setText(Double.toString(f.f2k(temp))); }
else if (from == 1 && to == 0) {
System.out.printf("Celsius to Fahrenheit, %.2f\n", f.c2f(temp));
//frame.l_result.setText(toString(f.c2f(temp)));}
[/cpp]
Think I at least know it's involving Double.toString(), but when I end it with .toString(), it keeps saying "double cannot be dereferenced." Actually, it's not even setting text.
[QUOTE=Mozartkugeln;38166679]Use two quotation marks to escape it. For example, "This is a ""string"" literal" should come out as:
[CODE]This is a "string" literal[/CODE][/QUOTE]
Already tried that, didn't work for some reason
[QUOTE=Topgamer7;38167655]Typically in regex you use back slash to escape characters. so you would put something like "[!%\"]" for your regex. I don't think you need the Regex.Escape part. But I'm not that great with VB[/QUOTE]
VB doesn't have the backslash escape character unfortunately
[QUOTE=Stonecycle;38169307][CODE]
formulae f = new formulae();
[B][U]final ui frame = new ui();[/U][/B]
double save;
[/CODE][/QUOTE]
OOP does not work like that. You're making a new, invisible [I]ui[/I] object every time that method is called, and you're putting it in a [B]local variable[/B]. You're actually [B]doing the same in the main method[/B]. If you want to be able to reference the frame you set-up outside the main method, you [B]must[/B] make it an instance field.
[cpp]
public static void main(String[] args) {
final ui frame = new ui();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// etc.
}
[/cpp]
becomes
[cpp]
private final ui frame = new ui();
public static void main(String[] args) {
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// etc.
}
[/cpp]
You will then be able to access the frame you see on the screen anywhere in the class, given that you don't [URL=http://en.wikipedia.org/wiki/Variable_shadowing]shadow[/URL] it by making local variables of the same name like you did in the [I]check()[/I] method (the line highlighted in the first code snippet, remove it from your code).
I also have several other gripes with your code: Since you're converting the results you get from the temperature conversion methods in each of the else-if blocks, why not put the [I]Double.toString()[/I] calls in those methods and then return the Strings? Also, I don't understand why you would need a separate class just for the formulas.
[QUOTE=Stonecycle;38169307]Think I at least know it's involving Double.toString(), but when I end it with .toString(), it keeps saying "double cannot be dereferenced." Actually, it's not even setting text.[/QUOTE]
Primitive data types don't have any methods; you, therefore, have to use the associated lang classes.
[cpp]
double d = 1.618
System.out.println(d.toString()) // Syntax error, won't even compile
System.out.println(Double.toString(d)) // Prints fine
[/cpp]
[editline]25th October 2012[/editline]
[QUOTE=Bazkip;38171894]Already tried that, didn't work for some reason [/QUOTE]
Hmm, have you tried doing it inside [I]Regex.Escape()[/I]?
snip
The inside of the FILE structure is nothing to do with you.
If you get given a non-NULL pointer then either a) you now have a valid file handle or b) your entire OS is broken.
Why are you even using FILE in C++
[QUOTE=esalaka;38177340]Why are you even using FILE in C++[/QUOTE]
Bad habit "/
It's working fine now after changing to fstream.
[QUOTE=Mozartkugeln;38176077]
I also have several other gripes with your code: Since you're converting the results you get from the temperature conversion methods in each of the else-if blocks, why not put the [I]Double.toString()[/I] calls in those methods and then return the Strings? Also, I don't understand why you would need a separate class just for the formulas.[/QUOTE]
I actually tried this, yet the JLabel won't update.
[cpp]frame.l_result.setText(Double.toString(f.c2k(temp)));[/cpp]
And this:
[cpp]
private final ui frame = new ui();
public static void main(String[] args) {
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// etc.
}
[/cpp]
won't work unless the main() isn't static, and it won't run if it's not a public static void main(String[] args).
[QUOTE=Stonecycle;38177872]I actually tried this, yet the JLabel won't update.[/QUOTE]
You're shadowing the [I]frame[/I] field with a local variable of the same name. Read my previous post again.
[QUOTE=Stonecycle;38177872]won't work unless the main() isn't static, and it won't run if it's not a public static void main(String[] args).[/QUOTE]
Then make a non-static method and call it from main? :V
[editline]25th October 2012[/editline]
Actually, post your whole class in its current state.
[QUOTE=Mozartkugeln;38179146]You're shadowing the [I]frame[/I] field with a local variable of the same name. Read my previous post again.
Then make a non-static method and call it from main? :V
[editline]25th October 2012[/editline]
Actually, post your whole class in its current state.[/QUOTE]
Managed to fix it somehow. Now the formatting of the JLabel's all I got left and that I think I can do on my own.
So after visiting WAYWO I decided to make my own color grid program ( its just a grid with each piece being a random color)
Using c++ with SFML. It works, but the problem is the CPU usage. If I limit my fps to 60 it still uses about 30%
My code:
[cpp]
#include <SFML\Graphics.hpp>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
//Initialization start
srand(time(0));
sf::RenderWindow window(sf::VideoMode(640,480),"Color");
window.setFramerateLimit(60);
vector<sf::RectangleShape> gridvec;
sf::RectangleShape grid;
grid.setSize(sf::Vector2f(10,10));
for ( int x = 0; x < 64; ++x )
{
for (int y = 0; y < 48; ++y )
{
grid.setPosition(x*10,y*10);
gridvec.push_back(grid);
}
}
for (int i = 0; i < gridvec.size();++i)
{
gridvec[i].setFillColor(sf::Color(rand()%256,rand()%256,rand()%256));
}
//Initialization end
//Main loop
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) //Generates new colors
{
for (int i = 0; i < gridvec.size();++i)
{
gridvec[i].setFillColor(sf::Color(rand()%256,rand()%256,rand()%256));
}
}
//Drawing time
window.clear();
for (int i = 0; i < gridvec.size();++i)
{
window.draw(gridvec[i]);
}
window.display();
}
}
[/cpp]
I guess it's because the rectangles are being redrawn so frequently. Any ideas how to lower it ?
Maybe not the best place to post this, but I was wondering: If I write a binding to a library that includes a function that loads and links the library, and put that binding in my compiler's Standard Library, do I have to change the license? Or does writing bindings not count as license infringement or whatever?
[QUOTE=Eudoxia;38184204]Maybe not the best place to post this, but I was wondering: If I write a binding to a library that includes a function that loads and links the library, and put that binding in my compiler's Standard Library, do I have to change the license? Or does writing bindings not count as license infringement or whatever?[/QUOTE]
It depends on the license and the linking method. IANAL, but if you're talking about the GPL, then I think it's that dynamic linking of the library is fine but static linking is not. More permissive (MIT, BSD, zlib, etc.) licenses don't care at all.
[QUOTE=robmaister12;38184313]It depends on the license and the linking method. IANAL, but if you're talking about the GPL, then I think it's that dynamic linking of the library is fine but static linking is not. More permissive (MIT, BSD, zlib, etc.) licenses don't care at all.[/QUOTE]
I think my compiler dynamically links things. 'Think' because this not-quite-compile-time-and-not-quite-run-time thing about JIT compilation always confuses me. Thanks.
Dynamic linking means that the licensed library is in it's own separate file. Static linking basically means that the entire library is copied into your library/executable.
[QUOTE=robmaister12;38185003]Dynamic linking means that the licensed library is in it's own separate file. Static linking basically means that the entire library is copied into your library/executable.[/QUOTE]
I think both are happening :I
How do I get SFML 1.6 (or 2.0) to work with GCC 4.7.0 and Code::Blocks 10.05, both of which are separate and not part of a package like the Code::Blocks/MinGW thing is?
I am having nothing but problems.
[QUOTE=elevate;38188433]How do I get SFML 1.6 (or 2.0) to work with GCC 4.7.0 and Code::Blocks 10.05, both of which are separate and not part of a package like the Code::Blocks/MinGW thing is?
I am having nothing but problems.[/QUOTE]
Hmm, it really shouldn't matter that they're not in a package together. As long as GCC can see it, Code::Blocks really has nothing to do with it. Code::Blocks just calls GCC, which then looks for SFML headers and libraries. You can put them directly in the GCC subfolders if you want (that's usually the easiest way).
Been trying to make a button in VB that can mute a program through the mixer, Anybody know how i could do this, Nothing is really working.
[QUOTE=ShaunOfTheLive;38188545]Hmm, it really shouldn't matter that they're not in a package together. As long as GCC can see it, Code::Blocks really has nothing to do with it. Code::Blocks just calls GCC, which then looks for SFML headers and libraries. You can put them directly in the GCC subfolders if you want (that's usually the easiest way).[/QUOTE]
Man I got it working. Had to build SFML 2.0 with MinGW w/ GCC 4.7.0.
For anyone who's interested: [url]http://sfmlcoder.wordpress.com/2011/06/16/building-sfml-2-0-with-mingw-make/[/url]
[QUOTE=demoTron;38182061]So after visiting WAYWO I decided to make my own color grid program ( its just a grid with each piece being a random color)
Using c++ with SFML. It works, but the problem is the CPU usage. If I limit my fps to 60 it still uses about 30%
My code:
[cpp]
#include <SFML\Graphics.hpp>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
//Initialization start
srand(time(0));
sf::RenderWindow window(sf::VideoMode(640,480),"Color");
window.setFramerateLimit(60);
vector<sf::RectangleShape> gridvec;
sf::RectangleShape grid;
grid.setSize(sf::Vector2f(10,10));
for ( int x = 0; x < 64; ++x )
{
for (int y = 0; y < 48; ++y )
{
grid.setPosition(x*10,y*10);
gridvec.push_back(grid);
}
}
for (int i = 0; i < gridvec.size();++i)
{
gridvec[i].setFillColor(sf::Color(rand()%256,rand()%256,rand()%256));
}
//Initialization end
//Main loop
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) //Generates new colors
{
for (int i = 0; i < gridvec.size();++i)
{
gridvec[i].setFillColor(sf::Color(rand()%256,rand()%256,rand()%256));
}
}
//Drawing time
window.clear();
for (int i = 0; i < gridvec.size();++i)
{
window.draw(gridvec[i]);
}
window.display();
}
}
[/cpp]
I guess it's because the rectangles are being redrawn so frequently. Any ideas how to lower it ?[/QUOTE]
You only need to redraw the rectangles if they've changed. Try shifting the clear() and draw() calls up into the if statement.
Sorry, you need to Log In to post a reply to this thread.