So i've been trying to get this function to draw a red box using turtle graphics but it seems like the function is being ignored. Here is my code:
int main(int agrc, char*argc[])
{
create_turtle_world();
void draw_red();
return (p1world_shutdown());
}
void draw_red()
{
pen_colour(RED);
forward(150);
turn(90);
forward(50);
turn(90);
forward(150)
turn(90);
forward(50);
turn(90);
turn(-90);
}
I don't know what I did wrong here, it's compiled correctly, just not drawing a box.
In main(), instead of
void draw_red(); //function declaration
you've to use
draw_red(); //function call
Related
#include "mbed.h"
#include <Callback.h>
InterruptIn up(p14);
void toggle1(int *player)
{
printf("%d \n", *player);
}
int main()
{
int player = 1;
up.rise(callback(toggle1, &player));
}
In the mbed callback function, why the result is not 1? It is 12784.
You are allowing your main function to return. After it returns, player will be out of scope so it might be overwritten with something else. In general, you never want to return from main in an embedded system, so I recommend adding while (1) {} at the end of your main function.
Also, using callback is a source of unnecessary complication and potential errors. I would just put player in a global variable (and mark it as volatile). Then you can simply do up.read(&toggle1).
I'm working on a game project which should be similar to a street fighter game.
The problem is that when I try to add new variables to the struct player which is inside a Abstract Data Type it crashes or the program doens't work propertly.
struct player {
//int neueVariable;
//int health;
float frameTime;
bool left;
float moveSpeed;
int curDoing;
int textureHeight;
int textureWidth;
SDL_Texture *texture;
SDL_Rect playerRect;
SDL_Rect playerPosition;
When I try to define the integer health it still works but some animations which are not connected to this abstract data type don't work anymore. So I guess it's some kind of a memory leak even though I have no Idea what it could be. But when I define the integer neueVariable and health (so both) it just crashes. Nothing works.
If you need some special code just tell me because I don't know what code you need because I have no clue what can cause it.
Im using the SDL library by the way.
#include "SDL.h"
#include "SDL_Image.h"
PPlayer player_create() {
PPlayer ply = (PPlayer) malloc(sizeof(struct player));
//player_setHealth(ply,100);
//player_setDead(ply,0);
player_setMoveSpeed(ply,0);
player_setCurDoing(ply,0);
player_setTextureHeight(ply,0);
player_setTextureWidth(ply,0);
player_setPlayerPositionX(ply,0);
player_setPlayerPositionY(ply,0);
player_setPlayerPositionW(ply,0);
player_setPlayerPositionH(ply,0);
player_setPlayerRectX(ply,0);
player_setPlayerRectY(ply,0);
player_setPlayerRectW(ply,0);
player_setPlayerRectH(ply,0);
return ply;
}
void player_setTextureHeight(PPlayer player, int value){
player->textureHeight = value;
}
in main for example
player_setTextureHeight(player1,rin_textureHeight);
rin_textureHeight is defined with int rin_textureHeight = 13600;
I need your suggestion to wrap my existing function.
I am from testing team I need to write unit test cases, so I don't want to depend on original definition so trying to write my own definiton.
Following is the source code which should not be changed.
source.c:
#include <stdio.h>
const char *getObjectName (int *anObject);
void func()
{
int *p;
getObjectName(p);
}
const char *getObjectName (int *anObject)
{
printf("i am in original\n");
}
From the above code I want to wrap getObjectName() function so that I can give my own definition.
I have googled a lot and tried following methods but didn't work out:
Method 1. using ld --wrap method
Method 2. using -DINTERCEPT
Method 3. using function pointer
I cannot use above 3 methods because calling function and called function are in same file.
So please suggest me any other methods to write my own defintion for getObjectName().
Disclaimer: asked over at perlmonks.
I hope I'm describing and depicting my issue properly... In XS, I'm trying to send a callback into an external library's function, where the callback has Perl specific functions. The XSUB is passed as a function pointer to an external C function. The XSUB callback being sent in turn calls back to a sub in the `main` perl application:
void callback(){
dSP;
PUSHMARK(SP);
call_pv("p_callback", G_DISCARD|G_NOARGS);
}
// example extern call
externFunc(&callback);
This segfaults. I think it's because the external library doesn't understand the perl functions that are being called. Things work fine if I call the C `callback()` function directly though.
Is there some magic that I can do to make the external library "see" the Perl C functions, or am I doing something wrong?
Here's the code I'm testing with:
use warnings;
use strict;
use Inline ('C' => 'DATA', libs => '-lwiringPi');
init();
setInterrupt(27, 3);
# direct call
callback();
# on() triggers the external function and sends
# it the callback
on(27);
sub p_callback {
print "in perl callback\n";
}
__DATA__
__C__
#include <stdlib.h>
#include <stdio.h>
#include <wiringPi.h>
void init();
void on(int pin);
void off(int pin);
void setInterrupt(int pin, int edge);
void callback();
void init(){
printf("in init\n");
wiringPiSetup();
}
void on(int pin){
pinMode(pin, 1);
digitalWrite(pin, 1);
}
void off(int pin){
digitalWrite(pin, 0);
pinMode(pin, 0);
}
void setInterrupt(int pin, int edge){
wiringPiISR(pin, edge, &callback);
}
void callback(){
dSP;
PUSHMARK(SP);
call_pv("p_callback", G_DISCARD|G_NOARGS);
}
Output:
in init
in perl callback
Segmentation fault
If I remove the perl specific C calls from within the callback and just do a `printf()` or other pure-C work, things proceed without a segfault.
Just came across this question and thought I'd give it my own answer as I did resolve it some time ago.
There were some important bits I was missing to set the Perl context, as well as within the C exec_perl_callback() function.
use warnings;
use strict;
use Inline 'C';
use Inline 'NoClean';
sub p_callback {
print "hello, world from perl!\n";
}
exec_perl_callback('p_callback');
__END__
__C__
#define PERL_NO_GET_CONTEXT
PerlInterpreter * mine;
void callback(char* perl_callback){
PERL_SET_CONTEXT(mine);
dSP;
ENTER;
SAVETMPS;
PUSHMARK(SP);
PUTBACK;
exec_perl_callback(perl_callback, G_DISCARD|G_NOARGS);
FREETMPS;
LEAVE;
}
Output:
hello world, from perl!
Why am I getting an error saying 'setPixel not defined' with this code?
#include <windows.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include<GL/glut.h>
inline int round(const float a)
{
return int (a+0.5);
}
void init(void)
{
glClearColor(0.0f,0.0f,1.0f,1.0f);
gluOrtho2D(0.0,200.0,0.0,200.0);
glMatrixMode(GL_PROJECTION);
}
void LineSegment(int xa, int ya,int xb,int yb)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f,0.0f,0.0f);
printf("Enter the initial value");
scanf("%d%d",&xa,&ya);
printf("Enter the final value");
scanf("%d%d",&xb,&yb);
int dx=xb-xa;
int dy=yb-ya;
int steps,k;
float xIncrement,yIncrement,x=xa,y=ya;
if(fabs(dx)>fabs(dy))
steps=fabs(dx);
else
steps=fabs(dy);
xIncrement=dx/(float)steps;
yIncrement=dy/(float)steps;
setPixel(round(x),round(y));
for(k=0;k<steps;k++);
{
x += xIncrement;
y += yIncrement;
setPixel(round(x),round(y));
}
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGBA);
glutCreateWindow("DDA Line Algorithm");
glutDisplayFunc(LineSegment);
init();
glutMainLoop();
return 0;
}
Because there is no setPixel method in OpenGL or GLUT and as far as I can see from your code, you do not define one either. OpenGL deals with rendering primitives like points, lines, triangels etc, but not directly with setting single pixels on the screen. Since it is unclear what you want to achieve some suggestions:
If you want to draw a line in OpenGL, use the appropriate methods like glBegin(GL_LINES), etc. (although they are deprecated and should not be used anymore.) or glDrawArrays(GL_LINES, ....
If the goal is to implement a dda software rasterizer, then you might have to write the pixels to a texture and then display this texture.
Because you haven't defined setPixel anywhere. It's not an OpenGL call. You need to write it yourself, and it should set pixels on a buffer (if you're using double buffering) which you then later use as an argument to glDrawPixels(), or a call to the display buffer using glVertex2i(x,y). You can see an example of both approaches here and here.
Also, your LineSegment function is broken. In OpenGL you call glutDisplayFunc to specify a function which is called as fast as possible to render the display. However, in this function you call scanf() to prompt the user for data - this is broken. You should prompt them once at the start, and then pass that data into the function (which will then run as often as possible once glutMainLoop is called).