i want that if i pressed '1', '2' '3' '4' '5', img, img2, img3 img4 img5 is loading.
but when i ran this code, i have 'nullpointerexception error' and 'The file "e,jpg" is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable.'
but i make a folder and named 'data'. then i made photo in 'data' folder and named 'ejpg'.
i can't understand why i have error.
how can i solve that.
The code below is what I made.
PFont myFont;
PImage img, img2, img3, img4, img5;
void setup(){
size(500, 500);
img = loadImage("a,jpg");
img2 = loadImage("b,jpg");
img3 = loadImage("c,jpg");
img4 = loadImage("d,jpg");
img5 = loadImage("e,jpg");
}
void draw(){
if(keyPressed == true){
if(key == '1'){
image(img, 0, 0, width, height);
}
else if(key == '2'){
image(img2, 0, 0, width, height);
}
else if(key == '3'){
image(img3, 0, 0, width, height);
}
else if(key == '4'){
image(img4, 0, 0, width, height);
}
else if(key == '5'){
image(img5, 0, 0, width, height);
}
}
}
You've put commas before the file extensions ",jpg" and so the files can't be loaded and the loadImage(String) function returns null. These should be dots: a.jpg, b.jpg, ...
Unless you have a different way of naming the files, then you just need to make sure the paths match the actual filenames.
It's a simple naming convention issue. If you have named the files "a", "b", "c" etc then you should address them as "a.jpg", "b.jpg", "c.jpg" and so on. Note the use of full stop/periods rather than commas - if you get this wrong, the files won't load.
If you have literally titled an image "ajpg", you would have to address it as "ajpg.jpg".
One more thing to note is to make sure you are using the correct file extension. For example, double check that the images are in fact .jpg and not .png files. It's surprising how easily this can trip you up.
Related
I wrote a font parser and renderer based on some functionality in the freetype library, but it currently only works for font files that I specify. I'd like to be able to retrieve fonts from the Windows Font Mapper and then parse and render them, but I can't figure out how to get the actual font data from a logical font.
I thought that the easy way to accomplish this would be to get the font file name from the logical font, but based on other stack overflow posts, there isn't any good solution to this because there isn't a 1 to 1 mapping of physical fonts to the logical font you specify.
This code shows how I'm currently retrieving a handle to a logical font:
HFONT windowsFont = CreateFontA(72, 0, 0, 0, 0,
0, 0, 0, ANSI_CHARSET,
OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH, "Arial");
HDC deviceContext = GetDC(windowHandle);
SelectObject(deviceContext, windowsFont);
DWORD fontSize = GetFontData(deviceContext, 0, 0, NULL, 0);
void *fontData = malloc(fontSize);
fontSize = GetFontData(deviceContext, 0, 0, fontData, 0);
char *fontFileData;
size_t fontFileSize;
// This is a function that I wrote that does what you'd expect. It opens
// a file, reads all the bytes to a buffer and closes the file
readFileToBuff(&fontFileData, &fontFileSize, "c:/windows/fonts/arial.ttf");
assert(fontFileSize == fontSize); // This passes
assert(((char)fontFileData) == ((char)fontData)); // This fails
Based on this stack overflow post which is for Java, I'm thinking that what I want to do may not be possible. It seems that the only solution may be to look at all the system font files and try to figure out what they are.
How to get ttf font data from system fonts in java
This surprises me though, because it seems that it would be relatively common for a program to want to render fonts themselves, without relying on the Windows renderer. Does anyone know of a way to get the font data, or how other people have solved this problem?
I don't understand why it's getting the exact same amount of bytes as
the file, but the data isn't the same.
From the official sample, I found the correct way to use GetFontData.
This is the modified code, and the returned data is the same as fontFileData.
HRESULT hr = S_OK;
LPVOID ptr = NULL;
HGLOBAL hGlobal = NULL;
...
HFONT windowsFont = CreateFontA(72, 0, 0, 0, 0,
0, 0, 0, ANSI_CHARSET,
OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH, "Arial");
HDC deviceContext = GetDC(hWnd);
SelectObject(deviceContext, windowsFont);
DWORD fontSize = GetFontData(deviceContext, 0, 0, NULL, 0);
hGlobal = GlobalAlloc(GMEM_MOVEABLE, fontSize);
ptr = GlobalLock(hGlobal);
if (!ptr)
{
hr = HRESULT_FROM_WIN32(GetLastError());
fwprintf(stderr, L"ERROR: Could not lock global memory object: %08X\n", hr);
}
else
{
if (GetFontData(deviceContext, 0, 0, ptr, fontSize) == GDI_ERROR)
{
fwprintf(stderr, L"ERROR: Could not get font data\n");
hr = E_UNEXPECTED;
}
GlobalUnlock(hGlobal);
}
char *fontFileData;
size_t fontFileSize;
// This is a function that I wrote that does what you'd expect. It opens
// a file, reads all the bytes to a buffer and closes the file
readFileToBuff(&fontFileData, &fontFileSize, "c:/windows/fonts/arial.ttf");
assert(fontFileSize == fontSize); // This passes
assert(((char)fontFileData) == ((char)fontData)); // This fails
I'm taking screenshots of windows using PrintWindow(). This works finde with the following code.
This works most of the time - I'm calling it very often in a continuos loop - but sometimes it fails. I started saving screenshots of the times it fails. Sometimes they are just black, but sometimes the ClientArea of the captured window is somehow offset and only visible on half the picture. Why is that the case?
EDIT:
Is the PrintWindow function guaranteed to work, even when its called ~100times a second? What makes me wonder is, that after every print window I'm actually verifying two pixels which default colors I stored as a COLORREF at startup. The program says they match almost all the time. Still, once I use GetPixel, the returned value from other pixels is simply black or white from time to time (which should never be the case). Once I save the HDC/HBITMAP as a screenshot (I called GdiFlush before), the screenshot is just plain black (which shouldn't be the case either, as I said, I checked two pixels before...).
Well for now, back to the initial question: Is this definitely my fault, or is it possible that PrintWindow just gives random results from time to time, without returning an error?
Ok, this is the NEW CODE:
/* Try printing the window 10times, in case it fails. */
for (int i = 0; i <= NUM_ITERATIONS_PREP; i++) {
releaseSurface();
prepared_HWND = hwnd;
window_dc = GetWindowDC(hwnd);
surface = CreateCompatibleDC(window_dc);
if (surface) {
RECT r;
GetWindowRect(hwnd, &r);
bmp = CreateCompatibleBitmap(window_dc, r.right - r.left, r.bottom - r.top);
if (!ReleaseDC(hwnd, window_dc)) {
writeLog("ReleaseDC failed.", black, true);
}
if (bmp) {
old_surface_bmp = SelectBitmap(surface, bmp);
//InvalidateRect(hwnd, 0, TRUE);
//UpdateWindow(hwnd);
int result = PrintWindow(hwnd, surface, 0);
if (result != 0) {
if (!verify || verifySurface())
break;
else {
writeLog("Surface could not be verified on iteration " + std::to_string(i) + ". Saving screenshot.", black, true);
saveDebugScreenshot();
QThread::msleep(2);
}
} else {
writeLog("PrintWindow() failed.", black, true);
}
}
else {
writeLog("BMP creation failed.", black, true);
}
}
else {
writeLog("HDC creation failed.", black, true);
}
}
And the new RELEASE CODE:
/* If you call prepareSurface(), call this once the surface isn't need anymore. */
void ClientProfile::releaseSurface() {
SelectObject(surface, old_surface_bmp);
DeleteDC(surface);
DeleteObject(bmp);
prepared_HWND = NULL;
}
releaseSurface() is called either at the beginning of prepareSurface() and additionally every time I'm done processing a table (this means its called once too often for every prepareSurface(). Is that a problem?).
Oh and old_surface_bmp, bmp and surface are declared as private class attributes.
Ive been up for 10 hours trying to figure out this. Ive tried out a bunch of different libraries to try to get ogg or mp3 files to play with allegro 4. I downloaded DUMB library and copied the include and lib files to mingw respective places (i use codeblocks).
What is the correct way to install a library to my compiler?
here is my code so far..
#include <allegro.h>
#include <iostream>
#include <stdlib.h>
#include <aldumb.h>
#include "CUSTOM_FUNCTIONS_HEADER.h"
#include "status_screen_functions_header.h"
#define WHITE makecol(255,255,255)
//General Game Settings
int volume = 128;
int pan = 128;
int pitch = 1000;
#
int main(int argc, char *argv[]){
allegro_init();
atexit(&dumb_exit);//initialize dumb
dumb_register_stdfiles(); //tELL DUMB HOW TO OPEN FILES
install_keyboard();
if(install_sound(DIGI_AUTODETECT, MIDI_NONE, "") != 0){
allegro_message("error initializing the sound system");
}
set_color_depth(16);
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 1100, 550, 0, 0);
write_status_screen("Status$>_Allegro Engine Initialization Successfull");
//******************************************************************************//
//*****************************************************************************//
//Write code below this line since allegro has been initialized//
BITMAP *dragon;
BITMAP *main_title, *main_menu;
BITMAP *buffer;
/*Menu music*/
// load_sample("OG_MUSIC\\straight_outta_compton_menu.ogg");
//Setup Audiostream for Main Menu Music//
write_status_screen("Status$> Loading OG Music Stream: \"Straight Outta Compton.OGG\"");
DUH *myduh;
myduh = dumb_load_it_quick("OG_MUSIC\\straight_outta_compton_menu.ogg");
AL_DUH_PLAYER *dp = al_start_duh(myduh, 2, 0,
volume, 4096, 48000);;
//logg_get_stream("straight_outta_compton_menu.ogg", volume, pan, true);
SAMPLE *menu_guntrigger = load_sample("OG_MUSIC\\gun_menu_choose.wav");
/*Menu count*/
int menct = 0;
buffer = create_bitmap(1100, 480);
textprintf_ex(screen, font, 0, 0, WHITE, 0, "Resolution = %ix%i ", SCREEN_W, SCREEN_H);
dragon = load_bitmap("BITMAPS\\COMPTON_START_MENU_FADED_TWO.bmp", NULL);
main_title = load_bitmap("BITMAPS\\OGM_TITLE.bmp", NULL);
main_menu = load_bitmap("BITMAPS\\MAIN_MENU.bmp", NULL);
write_status_screen("Status$>_Bitmaps loaded into memory!");
write_status_screen("Status$> Loading OG Sound Effects......:");
rest(650);
rest(700);
int frames_drawn = 0;
while(!key[KEY_ESC]){
/*Handle Controls First modulus 2 to make selection easier, every second frame, keypress is captured. DO NOT DO THIS FOR ACTUAL GAMEPLAY*/
if(key[KEY_UP] && frames_drawn % 2 == 0){
play_sample(menu_guntrigger, volume, pan, pitch, FALSE);
menct--;
} else if (key[KEY_DOWN] && frames_drawn % 3 == 0){
play_sample(menu_guntrigger, volume, pan, pitch, FALSE);
menct++;
}
if (menct > 3){
play_sample(menu_guntrigger, volume, pan, pitch, FALSE);
menct = 0;
}else if (menct <0){
play_sample(menu_guntrigger, volume, pan, pitch, FALSE);
menct = 3;
}
rest(10);
//Erase everything
clear(buffer);
//status box
rectfill(screen, 0, 480, 1100, 483, WHITE);
/*Draw the frame based on user input*/
draw_sprite(buffer, dragon, 0, 0);
draw_sprite(buffer, main_title, 315, 10);
drawframe(buffer, main_menu, 485, 145, 332, 305, 0, 0, 4, menct);
//blit(main_title, screen, 0, 0, 50, 50, 540, 96);
//masked_blit(main_title, screen, 0, 0, 50, 50, 540, 96);
//masked_blit(source, dest, framex, framey, x, y, width, height);
textprintf_ex(buffer, font, 900, 470, WHITE, 0, "Written By: Kelvin Silva", bitmap_color_depth(screen));
textprintf_ex(buffer, font, 0, 470, WHITE, 0, "Frames Drawn: %i", frames_drawn);
blit(buffer, screen, 0, 0, 0, 0, 1100, 480);
/*pollduh*/
al_poll_duh(dp);
/*Wait for vsync, rest a little, update framecount and prompt for more input*/
vsync();
frames_drawn++;
rest(50);
}
write_status_screen("Status$>_Game Loop Ended...");
clear_status_screen();
//NOTE NEED TO SETUP MEMORY DESTRUCTION
destroy_bitmap(dragon);
destroy_bitmap(main_title);
destroy_bitmap(buffer);
allegro_exit();
return 0;
}
END_OF_MAIN()
d:\programs\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\..\..\..\libaldmd.a(alplay.o)||In function `al_start_duh':|
D:\Users\Kelvin_Silva\Downloads\dumb-0.9.3\dumb-0.9.3\src\allegro\alplay.c|48|undefined reference to `al_assert'|
D:\Users\Kelvin_Silva\Downloads\dumb-0.9.3\dumb-0.9.3\src\allegro\alplay.c|49|undefined reference to `al_assert'|
D:\Users\Kelvin_Silva\Downloads\dumb-0.9.3\dumb-0.9.3\src\allegro\alplay.c|62|undefined reference to `play_audio_stream'|
D:\Users\Kelvin_Silva\Downloads\dumb-0.9.3\dumb-0.9.3\src\allegro\alplay.c|69|undefined reference to `voice_set_priority'|
D:\Users\Kelvin_Silva\Downloads\dumb-0.9.3\dumb-0.9.3\src\allegro\alplay.c|74|undefined reference to `stop_audio_stream'|
d:\programs\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\..\..\..\libaldmd.a(alplay.o)||In function `al_stop_duh':|
D:\Users\Kelvin_Silva\Downloads\dumb-0.9.3\dumb-0.9.3\src\allegro\alplay.c|92|undefined reference to `stop_audio_stream'|
d:\programs\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\..\..\..\libaldmd.a(alplay.o)||In function `al_pause_duh':|
D:\Users\Kelvin_Silva\Downloads\dumb-0.9.3\dumb-0.9.3\src\allegro\alplay.c|103|undefined reference to `voice_stop'|
d:\programs\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\..\..\..\libaldmd.a(alplay.o)||In function `al_resume_duh':|
D:\Users\Kelvin_Silva\Downloads\dumb-0.9.3\dumb-0.9.3\src\allegro\alplay.c|113|undefined reference to `voice_start'|
d:\programs\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\..\..\..\libaldmd.a(alplay.o)||In function `al_duh_set_priority':|
D:\Users\Kelvin_Silva\Downloads\dumb-0.9.3\dumb-0.9.3\src\allegro\alplay.c|123|undefined reference to `voice_set_priority'|
d:\programs\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\..\..\..\libaldmd.a(alplay.o)||In function `al_poll_duh':|
D:\Users\Kelvin_Silva\Downloads\dumb-0.9.3\dumb-0.9.3\src\allegro\alplay.c|156|undefined reference to `get_audio_stream_buffer'|
D:\Users\Kelvin_Silva\Downloads\dumb-0.9.3\dumb-0.9.3\src\allegro\alplay.c|166|undefined reference to `free_audio_stream_buffer'|
D:\Users\Kelvin_Silva\Downloads\dumb-0.9.3\dumb-0.9.3\src\allegro\alplay.c|167|undefined reference to `stop_audio_stream'|
D:\Users\Kelvin_Silva\Downloads\dumb-0.9.3\dumb-0.9.3\src\allegro\alplay.c|179|undefined reference to `free_audio_stream_buffer'|
d:\programs\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\..\..\..\libaldmd.a(alplay.o)||In function `al_duh_encapsulate_sigrenderer':|
D:\Users\Kelvin_Silva\Downloads\dumb-0.9.3\dumb-0.9.3\src\allegro\alplay.c|208|undefined reference to `al_assert'|
D:\Users\Kelvin_Silva\Downloads\dumb-0.9.3\dumb-0.9.3\src\allegro\alplay.c|209|undefined reference to `al_assert'|
D:\Users\Kelvin_Silva\Downloads\dumb-0.9.3\dumb-0.9.3\src\allegro\alplay.c|215|undefined reference to `play_audio_stream'|
D:\Users\Kelvin_Silva\Downloads\dumb-0.9.3\dumb-0.9.3\src\allegro\alplay.c|222|undefined reference to `voice_set_priority'|
d:\programs\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\..\..\..\libaldmd.a(alplay.o)||In function `al_duh_decompose_to_sigrenderer':|
D:\Users\Kelvin_Silva\Downloads\dumb-0.9.3\dumb-0.9.3\src\allegro\alplay.c|248|undefined reference to `stop_audio_stream'|
||=== Build finished: 18 errors, 0 warnings (0 minutes, 0 seconds) ===|
DUMB requires you to link with two libraries, the core libdumb and then the aldumb "glue". Looks like you are only linking with the latter. Order matters too.
Note that DUMB is used to play MOD files (and various variants). If you want ogg support, it comes built in Allegro 4.4 if you have the official ogg libraries installed.
Also, if you are just getting started, then you should really use Allegro 5. Version 4 isn't really supported any longer and doesn't work as well on modern operating systems.
I need help with canvas because of one problem that Im trying to solve for about a day. Here is part of code that I am testing.
var imgpos = 0;
function drawshape(context, shape, fill, bb) {
context.beginPath();
context.save();
context.translate( 0, 130);
context.scale(1, 0.65);
context.shadowOffsetX = 0;
context.shadowOffsetY = 10;
context.shadowBlur = 9;
context.shadowColor = "black";
context.arc(shape.x, shape.y, shape.r, shape.sa, shape.ea, shape.cc);
context.lineWidth = shape.lw;
// line color
context.strokeStyle = fill;
context.stroke();
context.restore();
// not working :( ---------------
context.save();
for(var lg = 0; lg < shape.ic; lg++) { //
var imagesel = new Image();
imagesel.src = '/images/themes/default/capsules/'+imgpos+'.png';
imagesel.onload = function(){
if(imgpos==0){xx=70;yy=320;}
if(imgpos==1){xx=120;yy=260;}
if(imgpos==2){xx=140;yy=320;}
if(imgpos==3){xx=160;yy=320;}
if(imgpos==4){xx=180;yy=320;}
if(imgpos==5){xx=200;yy=320;}
if(imgpos==6){xx=220;yy=320;}
if(imgpos==7){xx=240;yy=320;}
if(imgpos==8){xx=260;yy=320;}
context.drawImage(imagesel, xx, yy);
}
if(imgpos != 8){imgpos++;} else {imgpos=0;}
}
context.restore();
// till here :( ---------------
if(shape.link != 'Limited'){
context.save();
context.scale(1, 0.65);
context.translate(500,660);
context.font = "bold 15pt Arial";
context.fillStyle = "WHITE";
if(bb <= 2){
context.textAlign="right";
context.rotate((((shape.sa+shape.ea)-0.1)/2)-Math.PI);
context.fillText(shape.link, -170, 0);
}
if(bb > 2){
context.textAlign="left";
context.rotate((((shape.sa+shape.ea)+0.1)/2)-2*Math.PI);
context.fillText(shape.link, +170, 0);
}
context.restore();
}else{
context.save();
context.scale(1, 0.65);
context.translate( 0, 130);
context.textAlign="center";
context.font = "bold 15pt Arial";
context.fillStyle = "WHITE";
context.fillText(shape.link, shape.x, shape.y-10);
context.restore();
}
}
So this code (except of part not working) draws arcs in style of halfcircle, but each of them is separated and shaded etc... My problem is that I want to put pictures in all of them BUT not the same number of pictures (thats the reason for cycle <- must be right, tested and working with alert!...). In first should be one picture, in second two, in third one ... and finaly in ninth two. But if i try this code, pictures are all drowe on one place and all of them are changing position if this function runs... I dont know what to do with that..
Firstly I wanted to add them to the path (each of paths have link, but thats another function, working properly too), than I tryied to do it separated from that function but nothing worked for me. Important part of code is only not working part, averything else is working perfectly.
Thanks for help.
It looks like you're fooled by the infamous loop variable issue. JavaScript variables live per function, not per block. So, your imagesel / imgpos variables only exist once in fact, which means you only have one image at one location. The variable is overwritten each loop iteration.
You should wrap the appropriate loop code in a function to actually create separate variables. Also, you haven't declared xx / yy anywhere with var.
for(var lg = 0; lg < shape.ic; lg++) {
(function(imgpos) {
var imagesel = new Image();
imagesel.src = '/images/themes/default/capsules/'+imgpos+'.png';
imagesel.onload = function(){
if(imgpos==0){xx=70;yy=320;}
if(imgpos==1){xx=120;yy=260;}
if(imgpos==2){xx=140;yy=320;}
if(imgpos==3){xx=160;yy=320;}
if(imgpos==4){xx=180;yy=320;}
if(imgpos==5){xx=200;yy=320;}
if(imgpos==6){xx=220;yy=320;}
if(imgpos==7){xx=240;yy=320;}
if(imgpos==8){xx=260;yy=320;}
context.drawImage(imagesel, xx, yy);
}
})(imgpos);
if(imgpos != 8){imgpos++;} else {imgpos=0;}
}
In short, I want to write a Gnome-Shell-style window switcher. So I need to fetch snapshots of all the windows. My current program looks like this:
char filename[101];
sprintf(filename, "%d.png", (int)win_list[i]);
GdkWindow *win_gdk = gdk_x11_window_foreign_new_for_display
(gdk_display_get_default(), win_list[i]);
gint _w, _h;
gdk_drawable_get_size(GDK_DRAWABLE(win_gdk), &_w, &_h);
XEvent _xevent;
_xevent.xexpose =
(XExposeEvent)
{
.type = Expose,
.send_event = True,
.display = xsu_vars.dpy,
.window = win_list[i],
.x = 0, .y = 0, .width = _w, .height = _h,
.count = 0
};
XSendEvent(xsu_vars.dpy, win_list[i], False, 0, &_xevent);
GdkPixbuf *_pb = gdk_pixbuf_get_from_drawable(
NULL, GDK_DRAWABLE(win_gdk), NULL, 0, 0, 0, 0, _w, _h);
if(_pb != NULL) {
cairo_surface_t *_surf_cairo = cairo_image_surface_create(
CAIRO_FORMAT_RGB24, _w, _h);
cairo_t *_cr = cairo_create(_surf_cairo);
gdk_cairo_set_source_pixbuf(_cr, _pb, 0, 0);
cairo_paint(_cr);
cairo_surface_write_to_png(_surf_cairo, filename);
printf("%s saved successfully!\n", filename);
} else {
printf("failed...\n");
}
The program works well well, but it will not generate correct images for those windows which are on a different desktop of minimized -- they would look like this:
Note that I send a expose event to all windows before generating pixbufs of them.
UPDATE:
It seems that xlib doesn't support that. So the only way may be creating cache manually.
This is possible with Composite extension - see "Preventing the backing pixmap from being freed when the window is hidden/destroyed" section in tutorial.
Yes, your update is correct. When a window is unmapped (or covered up), X just discards its contents; they don't exist anywhere in order to be snapshotted.
I believe libwnck contains code to do this and other parts of writing a switcher, btw. It's basically a library for writing things like window switchers.